The Java Collections Framework (JCF) is a set of interfaces, classes, and algorithms that provide ready-made data structures for storing and manipulating groups of objects.
It includes:
-
Interfaces (List, Set, Map, Queue, etc.)
-
Implementations (ArrayList, HashSet, HashMap, LinkedList, etc.)
-
Utility Classes (Collections, Arrays)
Collections help with:
- Storing data
- Searching
- Sorting
- Insertion & deletion
- Manipulating dynamic data
Before Collections (Java 1.2), only arrays existed — fixed size and primitive-friendly only.
Collections provide:
- Dynamic size growth
- Built-in algorithms (sorting, searching)
- Thread-safe variants
- Better performance structures
- Consistent interfaces across data structures
| Term | Meaning |
|---|---|
| Collection | Root interface for all collection types |
| Collections | Utility class with static methods (sorting, reversing, syncing) |
Example:
Collections.sort(list);Stores elements in insertion order and allows duplicates.
| Class | Internal Structure | Allows Duplicates | Thread Safe | Best Use |
|---|---|---|---|---|
| ArrayList | Dynamic array | Yes | No | Fast read, random access |
| LinkedList | Doubly linked list | Yes | No | Frequent inserts/removals |
| Vector | Dynamic array | Yes | Yes | Legacy, thread-safe |
| Stack | Vector-based | Yes | Yes | Legacy stack operations |
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("A"); // duplicates allowedLinkedList<Integer> ll = new LinkedList<>();
ll.add(10);
ll.addFirst(5);
ll.addLast(20);Set does not allow duplicate values.
| Class | Order | Allows Null | Best Use |
|---|---|---|---|
| HashSet | No order | Yes | Fast lookup |
| LinkedHashSet | Maintains insertion order | Yes | Predictable iteration |
| TreeSet | Sorted (ascending) | No | Sorted data |
Set<Integer> set = new HashSet<>();
set.add(10);
set.add(20);
set.add(10); // ignoredSet<String> ts = new TreeSet<>();
ts.add("Banana");
ts.add("Apple");
ts.add("Mango");Output (sorted):
Apple, Banana, Mango
Used for FIFO (first-in-first-out) and other queue behaviors.
| Class | Description |
|---|---|
| LinkedList | Can act as Queue & Deque |
| PriorityQueue | Elements ordered by priority |
| ArrayDeque | Fast stack/queue implementation |
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq.poll()); // 10 (lowest)Maps store data as key → value.
Keys must be unique.
| Class | Order | Allows Null Keys | Allows Null Values | Best Use |
|---|---|---|---|---|
| HashMap | No order | Yes (1 null key) | Yes | Most common & fast |
| LinkedHashMap | Maintains insertion order | Yes | Yes | Cache implementations |
| TreeMap | Sorted by key | No | No | Sorted dictionaries |
| Hashtable | No order, synchronized | No | No | Legacy |
| ConcurrentHashMap | Thread-safe | No | No | High concurrency |
Map<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
map.put(1, "C"); // overrideMap<String, Integer> tm = new TreeMap<>();
tm.put("Banana", 2);
tm.put("Apple", 1);Sorted automatically:
Apple=1, Banana=2
java.util.Collections provides static helper methods.
| Method | Purpose |
|---|---|
sort() |
Sort list |
reverse() |
Reverse list |
shuffle() |
Randomize list |
min() |
Smallest element |
max() |
Largest element |
binarySearch() |
Search sorted list |
synchronizedList() |
Make thread-safe |
Example:
Collections.sort(list);
Collections.reverse(list);Used to traverse collection elements.
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}Can traverse forward and backward.
ListIterator<String> lit = list.listIterator();| Feature | List | Set | Map |
|---|---|---|---|
| Duplicates | Allowed | Not allowed | Keys not allowed |
| Order | Yes | Depends | No (unless LinkedHashMap/TreeMap) |
| Null | Yes | Yes/No | Key: 1, Value: many |
| Access | Index-based | No index | Key-based |
| Feature | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| Ordering | No | Insertion order | Sorted |
| Speed | Fastest | Medium | Slowest |
| Null allowed | Yes | Yes | No |
| Use | Fast lookup | Predictable iteration | Sorted data |
| Feature | ArrayList | LinkedList |
|---|---|---|
| Structure | Dynamic array | Doubly linked list |
| Read access | Fast | Slow |
| Insert/delete | Slow | Fast |
| Memory | Compact | More (pointers) |
| Feature | HashMap | Hashtable | ConcurrentHashMap |
|---|---|---|---|
| Thread-safe | No | Yes | Yes (better) |
| Null allowed | Yes | No | No |
| Performance | Fast | Slow | Fast under concurrency |
| Requirement | Recommended Collection |
|---|---|
| Indexed access | ArrayList |
| Many insert/delete operations | LinkedList |
| Unique elements (fast) | HashSet |
| Unique elements (sorted) | TreeSet |
| Key–value storage | HashMap |
| Sorted key–value | TreeMap |
| Thread-safe map | ConcurrentHashMap |
| FIFO queue | LinkedList / ArrayDeque |
| Priority-based | PriorityQueue |
