Collections Framework
The Java Collections Framework is a unified architecture of interfaces, implementations, and algorithms for working with groups of objects. Instead of writing your own data structures, you pick the right one from the framework.

Core Hierarchy
Section titled “Core Hierarchy”
Two separate hierarchies:
Collection<E>— for sequences of elements (List, Set, Queue)Map<K,V>— for key-value pairs (HashMap, TreeMap)


Performance at a Glance
Section titled “Performance at a Glance”| Implementation | Access | Search | Insert | Delete | Ordering | Thread-Safe |
|---|---|---|---|---|---|---|
| ArrayList | O(1) | O(n) | O(1)* | O(n) | Insertion | No |
| LinkedList | O(n) | O(n) | O(1) | O(1) | Insertion | No |
| HashSet | — | O(1) | O(1) | O(1) | None | No |
| TreeSet | — | O(log n) | O(log n) | O(log n) | Sorted | No |
| HashMap | O(1) | O(1) | O(1) | O(1) | None | No |
| TreeMap | O(log n) | O(log n) | O(log n) | O(log n) | Sorted | No |
*Amortized — occasional O(n) during resize


When to Use What
Section titled “When to Use What”What are you storing?
├── Key-Value pairs?│ ├── Need sorted keys? → TreeMap│ ├── Need insertion order? → LinkedHashMap│ └── Best performance? → HashMap│├── Unique elements only?│ ├── Need sorted order? → TreeSet│ ├── Need insertion order? → LinkedHashSet│ └── Best performance? → HashSet│├── Ordered elements (duplicates OK)?│ ├── Frequent random access? → ArrayList│ ├── Frequent insert/delete? → LinkedList│ └── Need thread safety? → Collections.synchronizedList()│└── Processing queue? ├── Priority-based? → PriorityQueue └── FIFO / both ends? → ArrayDequeCommon Initialization Patterns
Section titled “Common Initialization Patterns”// Diamond operator (Java 7+)List<String> list = new ArrayList<>();Map<String, Integer> map = new HashMap<>();
// Immutable factory methods (Java 9+)List<String> list = List.of("a", "b", "c");Set<Integer> set = Set.of(1, 2, 3);Map<String, Integer> map = Map.of("one", 1, "two", 2);
// Quick mutable list from arrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
// Remove duplicates from a listSet<String> unique = new HashSet<>(listWithDuplicates);
Useful Collections Utilities
Section titled “Useful Collections Utilities”Collections.sort(list);Collections.sort(list, comparator);Collections.reverse(list);int idx = Collections.binarySearch(sortedList, key);String min = Collections.min(collection);int count = Collections.frequency(collection, item);boolean noCommon = Collections.disjoint(coll1, coll2);