BackendBench
Coffee

Unravel › Java concepts › Collections and streams

Java language

The Collections utility class

Collections has static helpers for lists and sets: sort, reverse, shuffle, max, min, frequency, swap, plus wrappers that make a collection read-only, synchronized or empty.

Learn these first

At a glance

Mental model

A drawer of kitchen gadgets that work on any container: the peeler does not care what you are peeling.

What happens, in order
  1. 1Collections.sort(list) and list.sort(null) use the natural order; Collections.max(list, comparator) finds the biggest; Collections.frequency(list, x) counts occurrences
  2. 2Collections.emptyList(), singletonList(x) and nCopies(3, "ab") build small immutable lists without allocating much
  3. 3Collections.unmodifiableList(list) returns a read-only view; Collections.synchronizedList(list) wraps every call in a lock (iteration still needs manual synchronisation)
  4. 4Collections.binarySearch(sortedList, key) returns the position, or -(insertion point) - 1 if missing
Remember
Watch out
Thinking unmodifiableList is immutableCalling binarySearch on an unsorted listAdding to Collections.emptyList()
Example (highlighted lines explain it)
import java.util.*;public class Inventory {  private final List<String> items = new ArrayList<>();  public List<String> view() { return Collections.unmodifiableList(items); }  public String mostCommon() {    return Collections.max(new HashSet<>(items), Comparator.comparingInt(s -> Collections.frequency(items, s)));  }  public void tidy() { Collections.sort(items); Collections.reverse(items); }}
  1. line 1 uses , the core of The Collections utility class
  2. line 2 uses , the core of The Collections utility class
  3. line 3 uses , the core of The Collections utility class
  4. line 4 uses , the core of The Collections utility class
  5. line 5 uses , the core of The Collections utility class
  6. line 6 uses , the core of The Collections utility class
Read the full explanation (about 1 min)

Collections has static helpers for lists and sets: sort, reverse, shuffle, max, min, frequency, swap, plus wrappers that make a collection read-only, synchronized or empty.

A mental model

A drawer of kitchen gadgets that work on any container: the peeler does not care what you are peeling.

How it works

Collections.sort(list) and list.sort(null) use the natural order; Collections.max(list, comparator) finds the biggest; Collections.frequency(list, x) counts occurrences.

Collections.emptyList(), singletonList(x) and nCopies(3, "ab") build small immutable lists without allocating much.

Collections.unmodifiableList(list) returns a read-only view; Collections.synchronizedList(list) wraps every call in a lock (iteration still needs manual synchronisation).

Collections.binarySearch(sortedList, key) returns the position, or -(insertion point) - 1 if missing.

Going deeper

Unmodifiable wrappers are views: changes to the underlying list show through. For a real immutable snapshot, use List.copyOf.

Synchronized wrappers make single calls safe, but "check then act" sequences are not atomic. ConcurrentHashMap or CopyOnWriteArrayList is usually better.

Collections.shuffle(list, new Random(seed)) gives a reproducible shuffle for tests.

Under the hood

Collections.sort copies the list to an array, calls Arrays.sort (TimSort) and writes the elements back. Wrappers like UnmodifiableList just hold the target list and forward the read methods.

Common mistakes

  • Thinking unmodifiableList is immutable.
  • Calling binarySearch on an unsorted list.
  • Adding to Collections.emptyList().

How Unravel recognises it

When you paste code, Unravel looks for these signs of the collections utility class. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is The Collections utility class?

Collections has static helpers for lists and sets: sort, reverse, shuffle, max, min, frequency, swap, plus wrappers that make a collection read-only, synchronized or empty.

What are the most common mistakes with The Collections utility class?

Thinking unmodifiableList is immutable. Calling binarySearch on an unsorted list. Adding to Collections.emptyList().

How can I see The Collections utility class in real code?

Paste your own Java into Unravel and it will point out where the collections utility class appears, explain how it works at the detail level you choose, and draw the flow of the code around it.