Category : Java | Sub Category : Java.util Package | By Prasad Bonam Last updated: 2023-07-12 11:03:50 Viewed : 550
Here are examples of using List, Set, and Map in Java:
List Example:
javaimport java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// Create a List of strings
List<String> names = new ArrayList<>();
// Add elements to the List
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Access elements by index
System.out.println("Element at index 0: " + names.get(0));
// Update elements
names.set(1, "Brian");
System.out.println("Updated element at index 1: " + names.get(1));
// Remove elements
names.remove(2);
System.out.println("Size after removing an element: " + names.size());
// Iterate over the List
System.out.println("Elements in the List:");
for (String name : names) {
System.out.println(name);
}
}
}
Set Example:
javaimport java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// Create a Set of integers
Set<Integer> numbers = new HashSet<>();
// Add elements to the Set
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(20); // Adding a duplicate element, which will be ignored
// Access elements (no specific order in Set)
System.out.println("Size of the Set: " + numbers.size());
System.out.println("Is 20 present in the Set? " + numbers.contains(20));
// Remove elements
numbers.remove(10);
System.out.println("Size after removing an element: " + numbers.size());
// Iterate over the Set
System.out.println("Elements in the Set:");
for (int number : numbers) {
System.out.println(number);
}
}
}
Map Example:
javaimport java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
// Create a Map of names and ages
Map<String, Integer> ageMap = new HashMap<>();
// Add key-value pairs to the Map
ageMap.put("Alice", 25);
ageMap.put("Bob", 30);
ageMap.put("Charlie", 35);
// Access values by key
System.out.println("Age of Bob: " + ageMap.get("Bob"));
// Update values
ageMap.put("Bob", 32);
System.out.println("Updated age of Bob: " + ageMap.get("Bob"));
// Remove key-value pairs
ageMap.remove("Charlie");
System.out.println("Size after removing an element: " + ageMap.size());
// Iterate over the Map entries
System.out.println("Elements in the Map:");
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println("Name: " + entry.getKey() + ", Age: " + entry.getValue());
}
}
}
In these examples, we create instances of List (using ArrayList), Set (using HashSet), and Map (using HashMap). We perform operations such as adding elements, accessing elements, updating elements, removing elements, and iterating over the collections.
List maintains an ordered collection of elements with possible duplicates, Set stores a unique collection of elements without a specific order, and Map stores key-value pairs. Each collection has its own methods and characteristics for manipulating and accessing data.
You can customize the examples by changing the data types or using different implementations of List, Set, and Map, such as LinkedList, TreeSet, or LinkedHashMap, depending on your requirements.