`
The Java Collection Framework (JCF) is a set of classes and interfaces in the java.util package that provides data structures and algorithms for storing, retrieving, and manipulating collections of objects.
The Collection Framework has two main root interfaces:
Collection Interface:
Map Interface:
ArrayListLinkedListVectorStackHashSetLinkedHashSetTreeSetPriorityQueueArrayDequeHashMapLinkedHashMapTreeMapHashtableThe Collection interface is the root for most collection classes. Key operations include adding, removing, and iterating elements.
List is an ordered collection that allows duplicate elements.ArrayList: Dynamic array, fast random access, slower insertion/deletion.LinkedList: Doubly-linked list, faster insertion/deletion, slower random access.Vector: Synchronized (thread-safe) dynamic array.Stack: LIFO (Last In First Out) implementation of Vector.Set is an unordered collection that does not allow duplicates.HashSet: Fast operations, no ordering.LinkedHashSet: Maintains insertion order.TreeSet: Sorted set based on natural ordering or a custom comparator.Queue follows FIFO (First In First Out).
PriorityQueue: Maintains elements based on priority.Deque (Double-Ended Queue) allows insertion and deletion at both ends.
ArrayDeque: Array-based deque implementation.Map represents a collection of key-value pairs.HashMap: Unordered map, allows one null key and multiple null values.LinkedHashMap: Maintains insertion order.TreeMap: Sorted map based on natural ordering or a custom comparator.Hashtable: Synchronized, does not allow null keys or values.Collection Interface:| Method | Description |
|---|---|
add(E e) |
Adds an element to the collection. |
remove(Object o) |
Removes a single instance of the specified element. |
size() |
Returns the number of elements in the collection. |
clear() |
Removes all elements. |
contains(Object o) |
Checks if the collection contains the specified element. |
isEmpty() |
Checks if the collection is empty. |
iterator() |
Returns an iterator for traversing the collection. |
List Interface:| Method | Description |
|---|---|
get(int index) |
Returns the element at the specified position. |
set(int index, E element) |
Replaces the element at the specified position. |
add(int index, E element) |
Inserts an element at the specified position. |
remove(int index) |
Removes the element at the specified position. |
Map Interface:| Method | Description |
|---|---|
put(K key, V value) |
Associates the specified value with the specified key. |
get(Object key) |
Returns the value mapped to the specified key. |
remove(Object key) |
Removes the mapping for the specified key. |
containsKey(Object key) |
Checks if the map contains the specified key. |
containsValue(Object value) |
Checks if the map contains the specified value. |
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println("ArrayList: " + list);
list.remove("Python");
System.out.println("After removal: " + list);
}
}
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
set.add(1); // Duplicate, will not be added
System.out.println("HashSet: " + set);
}
}
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
System.out.println("HashMap: " + map);
System.out.println("Value for key 2: " + map.get(2));
map.remove(3);
System.out.println("After removal: " + map);
}
}
Vector and Hashtable are thread-safe.| Feature | ArrayList |
LinkedList |
HashSet |
HashMap |
|---|---|---|---|---|
| Order | Maintains insertion order | Maintains insertion order | No ordering | No ordering |
| Duplicates | Allowed | Allowed | Not allowed | Not allowed (keys) |
| Null Elements | Allowed | Allowed | Allows one | Allows one key |
| Performance | Fast random access | Fast insertion/deletion | Fast operations | Fast operations |
| Thread Safety | Not thread-safe | Not thread-safe | Not thread-safe | Not thread-safe |