Attic
Collections

Overview

Comprehensive working reference, hierarchy diagrams, and field guides for the Java Collections Framework

Almost everything in java.util answers to one of two root shapes:

  1. Collection<E>: A single group of elements that can be walked one at a time. It extends Iterable<E>, allowing every List, Set, and Queue to be traversed directly with a for-each loop.
  2. Map<K, V>: A separate root entirely. It does not extend Collection or Iterable, because a map's contract concerns key-to-value associations rather than a single stream of elements. Iteration occurs indirectly through its three live views: keySet(), values(), and entrySet().

Architectural Hierarchy

Live Views of a Map: map.keySet() -> Set<K>, map.values() -> Collection<V>, and map.entrySet() -> Set<Map.Entry<K,V>>. All three are live views; removing elements through any view immediately mutates the backing map.


Topic Categories


Quick Methods Summary: Arrays, Strings, Lists, Maps

Fast-lookup snapshot of essential method signatures across the four most heavily used structures:

Data StructurePrimary MethodsReturn TypeComplexityKey Feature / Gotcha
ArraysArrays.sort(arr)voidO(n log n)Dual-Pivot Quicksort for primitives, TimSort for objects
Arrays.binarySearch(arr, key)intO(log n)Precondition: array must be sorted
Arrays.copyOf(arr, newLen)T[]O(n)Zero-padded or truncated copy
Arrays.deepToString(matrix)StringO(R * C)Formats multidimensional 2D/3D grids recursively
Stringsstr.charAt(index)charO(1)Constant-time character lookup
str.substring(begin, end)StringO(k)Half-open slice [begin, end)
str.split(regex)String[]O(n)Regex split; escape dots as str.split("\\.")
sb.append(val) / reverse()StringBuilderO(1) amortized / O(n)Mutable buffer; avoid String += inside loops
Listslist.get(index) / set(i, val)EO(1) ArrayListIndex offset lookup in ArrayList
list.add(val)booleanO(1) amortizedAppends to tail; expands buffer by 50% on overflow
list.remove(index) vs (Object)E / booleanO(n)Trap: remove(2) removes index 2, not integer 2
list.subList(from, to)List<E>O(1)Live view [from, to) backed by the original list
Mapsmap.get(key) / put(key, val)VO(1) amortizedAverage constant-time key lookup and insertion
map.getOrDefault(key, default)VO(1) amortizedReturns fallback value if key is absent
map.computeIfAbsent(k, fn)VO(1) amortizedComputes & stores value if key absent (e.g. multi-map)
map.merge(k, val, biFn)VO(1) amortizedCleanest frequency map: map.merge(k, 1, Integer::sum)

Comprehensive Method Reference: For the complete toolkit tables with all parameters, return types, and code examples, see the Collections Master Cheatsheet.


Performance & Complexity Matrix

CollectionRandom AccessInsert (Head)Insert (Tail)Delete (Element)OrderingNull Keys / Elements
ArrayListO(1)O(n)O(1) amortizedO(n)Insertion orderYes
LinkedListO(n)O(1)O(1)O(1)Insertion orderYes
ArrayDequeO(1)O(1)O(1)O(n)FIFO / LIFONo (throws NPE)
HashSetO(1)O(1)O(1)O(1)Unordered1 null allowed
TreeSetO(log n)O(log n)O(log n)O(log n)Sorted / NaturalNo (throws NPE)
HashMapO(1)O(1)O(1)O(1)Unordered1 null key, any null values
TreeMapO(log n)O(log n)O(log n)O(log n)Sorted keysNo null keys

On this page