Java Programming Examples
Posted on Oct 17, 2024 in Computers
Sorting an ArrayList in Descending Order
Code Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class ArrayListSortDescending {
public static void main(String[] args) {
// Create an ArrayList to store Integer elements
ArrayList<Integer> list = new ArrayList<>();
// Create a Scanner object for input
Scanner scanner = new Scanner(System.in);
// Prompt user to enter the number of elements
System.out.print("Enter the number of elements you want to add: ");
int numberOfElements = scanner.nextInt();
// Prompt user to enter the elements
System.out.println("Enter the elements:");
for (int i = 0; i < numberOfElements; i++) {
int element = scanner.nextInt();
list.add(element);
}
// Sort the list in descending order using Collections.reverseOrder()
Collections.sort(list, Collections.reverseOrder());
// Display the sorted list
System.out.println("Sorted list in descending order:");
for (int element : list) {
System.out.println(element);
}
// Close the scanner
scanner.close();
}
}
Creating Singleton Collections
Code Example
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SingletonCollectionExample {
public static void main(String[] args) {
// Create a singleton set
Set<String> singletonSet = Collections.singleton("singleElement");
System.out.println("Singleton Set: " + singletonSet);
// Create a singleton list
List<String> singletonList = Collections.singletonList("singleElement");
System.out.println("Singleton List: " + singletonList);
// Create a singleton map
Map<String, String> singletonMap = Collections.singletonMap("key", "value");
System.out.println("Singleton Map: " + singletonMap);
}
}
Creating Unmodifiable Collections
Code Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UnmodifiableCollectionExample {
public static void main(String[] args) {
// Create a modifiable list
List<String> modifiableList = new ArrayList<>();
modifiableList.add("element1");
modifiableList.add("element2");
// Create an unmodifiable view of the list
List<String> unmodifiableList = Collections.unmodifiableList(modifiableList);
System.out.println("Unmodifiable List: " + unmodifiableList);
// Attempting to modify the unmodifiable list will throw an exception
try {
unmodifiableList.add("element3");
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify an unmodifiable list: " + e);
}
// However, modifications to the original list are reflected in the unmodifiable view
modifiableList.add("element3");
System.out.println("Unmodifiable List after modifying the original list: " + unmodifiableList);
}
}
Drawing a Circle with JavaFX
Code Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class CircleInSceneExample extends Application {
@Override
public void start(Stage primaryStage) {
// Create a Circle
Circle circle = new Circle(50); // Radius of 50
circle.setFill(Color.RED); // Fill the circle with red color
// Create a layout container (StackPane) and add the circle to it
StackPane root = new StackPane();
root.getChildren().add(circle);
// Create a Scene with the StackPane as the root node
Scene scene = new Scene(root, 300, 200); // 300x200 scene size
// Set the scene in the stage
primaryStage.setScene(scene);
primaryStage.setTitle("Circle in Scene Example");
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args); // Launch the JavaFX application
}
}
Handling Events with JavaFX
Code Example
// ... (rest of the provided code examples) ...