Collections Utility Class — sort, shuffle, frequency, unmodifiable
Fast recall
Quick revision
Core answer
Collection instances. Most-used methods: `Collections.sort(list)` — TimSort, O(n log n), stable; `Collections.sort(list, comparator)` — custom order; `Collections.reverse(list)` — in place; `Collections.shuffle(list)` — Fisher-Yates random shuffle; `Collections.binarySearch(sortedList, key)` — O(log n) for RandomAccess lists; `Collections.frequency(coll, obj)` — count occurrences; `Collections.disjoint(c1, c2)` — true if no common elements; `Collections.min/max(coll)` — natural order or comparator; `Collections.unmodifiableList/Set/Map(coll)` — read-only view (throws on modify); `Collections.synchronizedList/Set/Map(coll)` — thread-safe wrapper (use ConcurrentHashMap instead for new code); `Collections.emptyList/Set/Map()` — immutable empty singletons; `Collections.singletonList(item)` — immutable single-item list. Modern Java (9+) replacements: prefer `List.of(...)`, `Set.of(...)`, `Map.of(...)` for immutable collections (truly immutable, not 'unmodifiable view'); prefer `list.sort(comparator)` over Collections.sort(list, comparator); prefer `Collectors.toList()`/`toSet()`/`toMap()` for stream collection. For interviews: mention sort/shuffle/binarySearch as classics, but flag that List.of/Set.of/Map.of and instance methods like list.sort have largely replaced the static Collections API.Knowledge of the Collections utility class methods and when to use them versus stream-based alternatives
Confusing Collections.sort() with Arrays.sort(), or not knowing that Collections.unmodifiableList() creates a view not a copy
Mention that Collections.singletonList() returns a truly immutable single-element list, and that Collections.nCopies() is useful for initializing lists
Interview-ready guide
Interview answer
The java.util.Collections class provides static utility methods for sorting, searching, shuffling, and creating special-purpose collection wrappers. Several of its methods have been superseded by cleaner APIs introduced in Java 8-16, but a handful remain uniquely useful without equivalents elsewhere.
Superseded — prefer modern alternatives:
Collections.sort()→list.sort(comparator)(Java 8)Collections.unmodifiableList()→List.copyOf()for true immutability (Java 10)Collections.synchronizedMap()→ConcurrentHashMapfor concurrent access with lock-free reads
Still valuable:
frequency(collection, element): count occurrences in any Collectiondisjoint(c1, c2): check if two collections share no common elementsnCopies(n, element): immutable list of repeated elements — useful for initialization and paddingcheckedList()/checkedMap(): add runtime type enforcement — useful for debugging raw-type pollution in legacy code
singletonList() vs List.of():
List.of(element)is now preferred — rejects null;singletonList()allows null
I recommend knowing which methods have modern replacements and which remain uniquely useful — the frequency(), disjoint(), and checked wrapper methods have no direct equivalent elsewhere.
Collections Utility Class — sort, shuffle, frequency, unmodifiable
Direct Answer
java.util.Collections is a static utility class providing algorithms (sort, shuffle, reverse, binarySearch), wrappers (unmodifiable, synchronized, checked), and convenience methods (frequency, disjoint, min, max, emptyList, singletonList) for working with Collection instances. Most-used methods: Collections.sort(list) — TimSort, O(n log n), stable; Collections.sort(list, comparator) — custom order; Collections.reverse(list) — in place; Collections.shuffle(list) — Fisher-Yates random shuffle; Collections.binarySearch(sortedList, key) — O(log n) for RandomAccess lists; Collections.frequency(coll, obj) — count occurrences; Collections.disjoint(c1, c2) — true if no common elements; Collections.min/max(coll) — natural order or comparator; Collections.unmodifiableList/Set/Map(coll) — read-only view (throws on modify); Collections.synchronizedList/Set/Map(coll) — thread-safe wrapper (use ConcurrentHashMap instead for new code); Collections.emptyList/Set/Map() — immutable empty singletons; Collections.singletonList(item) — immutable single-item list. Modern Java (9+) replacements: prefer List.of(...), Set.of(...), Map.of(...) for immutable collections (truly immutable, not 'unmodifiable view'); prefer list.sort(comparator) over Collections.sort(list, comparator); prefer Collectors.toList()/toSet()/toMap() for stream collection. For interviews: mention sort/shuffle/binarySearch as classics, but flag that List.of/Set.of/Map.of and instance methods like list.sort have largely replaced the static Collections API.
Deep Dive & Architecture
compute, merge, computeIfAbsent, and computeIfPresent are Map methods (Java 8+) that atomically combine get + check + put into a single operation, replacing the verbose get-check-put anti-pattern. computeIfAbsent(key, mappingFunction) — if key absent, compute value and put; most-used for lazy initialization of multi-valued maps: map.computeIfAbsent(key, k -> new ArrayList<>()).add(value). computeIfPresent(key, remappingFunction) — if key present, recompute value (or remove if null returned). compute(key, remappingFunction) — recompute regardless of presence (function gets old value or null). merge(key, value, remappingFunction) — if absent, put value; if present, apply function to combine: map.merge(word, 1, Integer::sum) is the idiomatic word-counter. Why they matter: (1) atomic in ConcurrentHashMap — no lost updates from concurrent get-check-put races; (2) less code than if (map.containsKey(k)) map.put(k, ...) else map.put(k, ...); (3) cleaner intent — the operation says what it does. Pitfalls: (1) not atomic in non-concurrent maps (HashMap) — only the API is single-call, not thread-safe; (2) returning null from a remapping function removes the key — surprising; (3) don't recursively modify the map inside the function — undefined behavior; (4) merge doesn't handle null values the same way as compute — read the Javadoc. For interviews: lead with 'replaces get-check-put, atomic in ConcurrentHashMap, idiomatic for multimap lazy-init and counters'.
java.util.Collections is a static utility class providing algorithms (sort, shuffle, reverse, binarySearch), wrappers (unmodifiable, synchronized, checked), and convenience methods (frequency, disjoint, min, max, emptyList, singletonList) for working with Collection instances. Most-used methods: Collections.sort(list) — TimSort, O(n log n), stable; Collections.sort(list, comparator) — custom order; Collections.reverse(list) — in place; Collections.shuffle(list) — Fisher-Yates random shuffle; Collections.binarySearch(sortedList, key) — O(log n) for RandomAccess lists; Collections.frequency(coll, obj) — count occurrences; Collections.disjoint(c1, c2) — true if no common elements; Collections.min/max(coll) — natural order or comparator; Collections.unmodifiableList/Set/Map(coll) — read-only view (throws on modify); Collections.synchronizedList/Set/Map(coll) — thread-safe wrapper (use ConcurrentHashMap instead for new code); Collections.emptyList/Set/Map() — immutable empty singletons; Collections.singletonList(item) — immutable single-item list. Modern Java (9+) replacements: prefer List.of(...), Set.of(...), Map.of(...) for immutable collections (truly immutable, not 'unmodifiable view'); prefer list.sort(comparator) over Collections.sort(list, comparator); prefer Collectors.toList()/toSet()/toMap() for stream collection. For interviews: mention sort/shuffle/binarySearch as classics, but flag that List.of/Set.of/Map.of and instance methods like list.sort have largely replaced the static Collections API.
A common mistake is confusing the interface with the implementation (e.g., saying List when you mean ArrayList), or not knowing that Map does not extend Collection.
Common Mistakes & Pitfalls
Confusing Collections.sort() with Arrays.sort(), or not knowing that Collections.unmodifiableList() creates a view not a copy
Check your understanding
Answer these without looking back at the article.
- What is the Java Collections Framework (JCF) and why does it exist? It provides a unified architecture of interfaces and implementations for storing and manipulating groups of objects, replacing the ad-hoc pre-JCF classes (Vector, Hashtable, Enumeration) with a consistent, interoperable API.
- How does HashSet work internally in Java?
- How does HashMap internally handle hash collisions in Java 8+? Explain the transition from linked list to tree.
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.