The Java Collections Framework is built around a set of core interfaces. These interfaces define the behavior of different types of collections. The main interfaces are:
Collection
: The root interface in the collection hierarchy. It represents a group of objects, known as elements.List
: An ordered collection that can contain duplicate elements.Set
: A collection that cannot contain duplicate elements.Queue
: A collection used for holding elements prior to processing.Map
: An object that maps keys to values. It does not inherit from the Collection
interface but is an integral part of the Collections Framework.There are many classes that implement these interfaces. Some of the commonly used classes are:
ArrayList
: Implements the List
interface. It uses a dynamic array to store elements.HashSet
: Implements the Set
interface. It uses a hash table to store elements.LinkedList
: Implements the List
and Deque
interfaces. It uses a doubly - linked list to store elements.HashMap
: Implements the Map
interface. It uses a hash table to store key - value pairs.List
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
// Create a new ArrayList
List<String> list = new ArrayList<>();
// Add elements to the list
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// Access elements
System.out.println("Element at index 1: " + list.get(1));
// Iterate over the list
for (String fruit : list) {
System.out.println(fruit);
}
// Remove an element
list.remove("Banana");
System.out.println("List after removing Banana: " + list);
}
}
In this example, we create an ArrayList
of strings. We add elements to the list, access an element by its index, iterate over the list using a for - each loop, and remove an element from the list.
Set
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// Create a new HashSet
Set<String> set = new HashSet<>();
// Add elements to the set
set.add("Dog");
set.add("Cat");
set.add("Dog"); // This duplicate will not be added
// Iterate over the set
for (String animal : set) {
System.out.println(animal);
}
// Check if an element exists
System.out.println("Does the set contain Cat? " + set.contains("Cat"));
}
}
Here, we create a HashSet
of strings. When we try to add a duplicate element, it is not added to the set. We then iterate over the set and check if an element exists in the set.
Map
import java.util.HashMap;
import java.util.Map;
public class MapExample {
public static void main(String[] args) {
// Create a new HashMap
Map<String, Integer> map = new HashMap<>();
// Add key - value pairs to the map
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Get a value by key
System.out.println("Value associated with key 'Two': " + map.get("Two"));
// Iterate over the map
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Remove a key - value pair
map.remove("Three");
System.out.println("Map after removing 'Three': " + map);
}
}
In this code, we create a HashMap
with string keys and integer values. We add key - value pairs, get a value by its key, iterate over the map using the entrySet()
, and remove a key - value pair.
List
like ArrayList
or LinkedList
.Set
like HashSet
or TreeSet
(if you need sorted elements).Map
like HashMap
or TreeMap
(if you need sorted keys).Generics allow you to specify the type of elements that a collection can hold. This helps in preventing runtime type errors and makes the code more readable. For example:
List<String> stringList = new ArrayList<>();
This ensures that only String
objects can be added to the ArrayList
.
List
and Set
, you can use a for - each loop or an Iterator
.Map
, you can use the entrySet()
, keySet()
, or values()
methods to iterate over key - value pairs, keys, or values respectively.When creating a ArrayList
or HashMap
, you can specify an initial capacity. This can improve performance if you know approximately how many elements the collection will hold.
List<String> list = new ArrayList<>(100);
Map<String, Integer> map = new HashMap<>(200);
Immutable collections are thread - safe and can prevent accidental modification. Java 9 introduced factory methods to create immutable collections.
import java.util.List;
public class ImmutableListExample {
public static void main(String[] args) {
List<String> immutableList = List.of("A", "B", "C");
// The following line will throw an UnsupportedOperationException
// immutableList.add("D");
}
}
If you are using collections in a multi - threaded environment, you need to ensure thread - safety. You can use synchronized collections like Collections.synchronizedList()
or concurrent collections like ConcurrentHashMap
.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SynchronizedListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
List<String> synchronizedList = Collections.synchronizedList(list);
}
}
The Java Collections Framework is a powerful and versatile tool for managing collections of data in Java. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can write more efficient, reliable, and maintainable code. Whether you are working with simple lists, sets, or complex maps, the Java Collections Framework provides the necessary functionality to handle your data effectively.