Title here
Summary here
Let’s dive deep into one of the most fundamental pillars of Java:
The Java Collections Framework (JCF) is a unified architecture for representing and manipulating groups of objects.
It includes interfaces, classes, and algorithms to store, retrieve, and process data efficiently.
Advantage | Description |
---|---|
Reusable structures | Ready-made dynamic data structures like List, Set, Map |
Optimized performance | Backed by efficient algorithms |
Type safety + Generics | You can store specific types (no casting) |
Unified architecture | Easy to learn, extend, and use |
Interface | Purpose | Key Implementations |
---|---|---|
Collection | Root interface for groups of elements | List, Set, Queue |
List | Ordered collection (duplicates allowed) | ArrayList, LinkedList |
Set | Unique elements only | HashSet, LinkedHashSet, TreeSet |
Queue | FIFO structures | PriorityQueue, ArrayDeque |
Map | Key-value pairs | HashMap, TreeMap, LinkedHashMap |
Ordered, indexed, allows duplicates
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("A"); // duplicates allowed
System.out.println(list.get(1)); // Output: B
Class | Feature |
---|---|
ArrayList | Resizable array, fast random access |
LinkedList | Doubly linked list, fast insertion/removal |
Vector | Synchronized (legacy, rarely used) |
Unordered, no duplicates
Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("A"); // ignored
Class | Feature |
---|---|
HashSet | Fast, no order |
LinkedHashSet | Maintains insertion order |
TreeSet | Sorted order (uses Red-Black tree) |
FIFO behavior, good for scheduling and processing
Queue<String> queue = new LinkedList<>();
queue.offer("A");
queue.offer("B");
System.out.println(queue.poll()); // A
Class | Feature |
---|---|
LinkedList | Implements both Queue and Deque |
PriorityQueue | Elements sorted by priority |
Key-value pairs, keys must be unique
Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(1, "C"); // overwrites value for key 1
System.out.println(map.get(1)); // Output: C
Class | Feature |
---|---|
HashMap | Fast, unordered |
LinkedHashMap | Maintains insertion order |
TreeMap | Sorted by key (uses Red-Black tree) |
Hashtable | Synchronized legacy version (avoid using in new code) |
Feature | ArrayList | LinkedList | HashSet | TreeSet | HashMap | TreeMap |
---|---|---|---|---|---|---|
Ordering | Index-based | Linked | Unordered | Sorted | Unordered | Sorted |
Allows Duplicates | Yes | Yes | No | No | Keys: No, Values: Yes | Same |
Nulls | Allowed | Allowed | One null | No nulls | One null key | No nulls |
Performance | Fast access | Fast insert/remove | Fast | Slower | Fast | Slower |
List
when order and duplicates matter. Use Set
when uniqueness is required.Collection
. Itβs a different design (key-value mapping).Comparator
.Collections
classCollections.sort(list);
Collections.reverse(list);
Collections.shuffle(list);
Collections.max(list);
Collections.min(list);
Collections.frequency(list, "A");
List
instead of ArrayList
).List<String> readOnly = Collections.unmodifiableList(list);
ArrayList<String>
, add 5 names.Set<Integer>
with some duplicate values.Map<String, Integer>
for employee names and salaries.TreeSet<String>
and add 5 names. Observe the sorting.TreeMap<Integer, String>
with roll numbers as keys.Write code to insert 1 million integers into:
ArrayList
LinkedList
HashSet
Compare the time taken using System.nanoTime()
.Would you like to dive next into Concurrent Collections, or go into File I/O (java.nio or java.io), or Multithreading Basics next?