Java Collections Framework provides classes and interfaces to store, retrieve, and manipulate groups of objects efficiently. Unlike arrays, collections can grow dynamically and provide built-in methods for operations like sorting, searching, and iteration.
1. Key Interfaces in Java Collections
| Interface | Description |
|---|---|
Collection |
Root interface for all collections (except Map). |
List |
Ordered collection (allows duplicates). |
Set |
Unordered collection (no duplicates). |
Queue |
Follows FIFO (First-In-First-Out) order. |
Deque |
Double-ended queue (supports FIFO & LIFO). |
Map |
Stores key-value pairs (not part of Collection but part of Collections Framework). |
2. Key Classes in Java Collections
| Interface | Implementations |
|---|---|
List |
ArrayList, LinkedList, Vector, Stack
|
Set |
HashSet, LinkedHashSet, TreeSet
|
Queue |
PriorityQueue, ArrayDeque
|
Map |
HashMap, LinkedHashMap, TreeMap, Hashtable
|
3. List (Ordered, Allows Duplicates)
3.1 ArrayList
- Dynamic array (resizable).
- Fast random access (O(1)).
- Slower insertions/deletions in the middle (O(n)).
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list); // [Apple, Banana, Cherry]
3.2 LinkedList
- Doubly-linked list.
- Faster insertions/deletions (O(1)).
- Slower random access (O(n)).
List<Integer> linkedList = new LinkedList<>();
linkedList.add(10);
linkedList.add(20);
linkedList.addFirst(5); // Adds at the beginning
System.out.println(linkedList); // [5, 10, 20]
3.3 Vector (Thread-Safe but Legacy)
- Similar to
ArrayListbut synchronized (thread-safe). - Slower due to synchronization.
Vector<String> vec = new Vector<>();
vec.add("One");
vec.add("Two");
System.out.println(vec); // [One, Two]
3.4 Stack (LIFO - Last-In-First-Out)
- Extends
Vector. -
push(),pop(),peek()methods.
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20 (removes & returns)
System.out.println(stack.peek()); // 10 (only retrieves)
4. Set (No Duplicates, Unordered)
4.1 HashSet
-
Uses
HashMapinternally. - No order (fastest for add/remove/contains).
- O(1) average time complexity.
Set<String> set = new HashSet<>();
set.add("Dog");
set.add("Cat");
set.add("Dog"); // Duplicate ignored
System.out.println(set); // [Cat, Dog] (order not guaranteed)
4.2 LinkedHashSet
- Maintains insertion order.
- Slower than
HashSetbut ordered.
Set<Integer> linkedSet = new LinkedHashSet<>();
linkedSet.add(30);
linkedSet.add(10);
linkedSet.add(20);
System.out.println(linkedSet); // [30, 10, 20] (insertion order)
4.3 TreeSet
- Sorted (natural order or custom comparator).
-
O(log n) for operations (uses
TreeMap).
Set<Integer> treeSet = new TreeSet<>();
treeSet.add(50);
treeSet.add(20);
treeSet.add(80);
System.out.println(treeSet); // [20, 50, 80] (sorted)
5. Queue (FIFO - First-In-First-Out)
5.1 PriorityQueue
- Orders elements based on priority (min-heap by default).
-
No
nullallowed.
Queue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq.poll()); // 10 (removes smallest first)
5.2 ArrayDeque (Faster than LinkedList for Queue ops)
- Double-ended queue (FIFO & LIFO).
-
Faster than
LinkedListfor queue operations.
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("First");
deque.addLast("Last");
System.out.println(deque); // [First, Last]
6. Map (Key-Value Pairs)
6.1 HashMap
-
No order, allows
nullkey/value. -
O(1) average time for
get(),put().
Map<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
System.out.println(map.get("Alice")); // 25
6.2 LinkedHashMap
- Maintains insertion order.
Map<String, Integer> linkedMap = new LinkedHashMap<>();
linkedMap.put("Zebra", 1);
linkedMap.put("Apple", 2);
System.out.println(linkedMap); // {Zebra=1, Apple=2} (insertion order)
6.3 TreeMap
- Sorted by keys (natural/comparator order).
- O(log n) for operations.
Map<String, Integer> treeMap = new TreeMap<>();
treeMap.put("Banana", 3);
treeMap.put("Apple", 1);
System.out.println(treeMap); // {Apple=1, Banana=3} (sorted)
6.4 Hashtable (Legacy, Thread-Safe)
- Synchronized (thread-safe).
-
No
nullkeys/values allowed.
Hashtable<Integer, String> table = new Hashtable<>();
table.put(1, "One");
table.put(2, "Two");
System.out.println(table.get(1)); // "One"
7. Collections Utility Class
Provides useful methods like sort(), reverse(), shuffle(), etc.
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 9, 1));
Collections.sort(numbers); // [1, 3, 5, 9]
Collections.reverse(numbers); // [9, 5, 3, 1]
Collections.shuffle(numbers); // Random order
8. When to Use Which Collection?
| Requirement | Best Collection Choice |
|---|---|
| Fast access by index | ArrayList |
| Frequent insertions/deletions | LinkedList |
| No duplicates, fast lookup | HashSet |
| No duplicates, sorted | TreeSet |
| FIFO processing |
PriorityQueue / ArrayDeque
|
| Key-value pairs, fast lookup | HashMap |
| Key-value pairs, sorted | TreeMap |
| Thread-safe operations |
Vector, Hashtable, ConcurrentHashMap
|
9. Summary
✔ List → Ordered, allows duplicates (ArrayList, LinkedList)
✔ Set → No duplicates (HashSet, TreeSet, LinkedHashSet)
✔ Queue → FIFO (PriorityQueue, ArrayDeque)
✔ Map → Key-value pairs (HashMap, TreeMap, LinkedHashMap)
✔ Collections class → Utility methods (sort, reverse, shuffle)
Java Collections Framework is powerful and flexible, allowing efficient data storage and manipulation. Choose the right collection based on your needs! 🚀
Top comments (0)