JavaThe Collections Class

The Collections Class

Collections vs. Collection
java.util.Collections (plural, a class) is easy to confuse with java.util.Collection (singular, an interface). Collection is the root interface implemented by List, Set, and friends. Collections is an entirely different thing: a utility class full of static helper methods that operate on those collections.
Collections works the same way Arrays does for arrays — it's a grab-bag of static methods for sorting, searching, wrapping, and creating small special-purpose collections, none of which belong on the Collection interface itself.
sort(), reverse(), max(), and min()
Collections.sort() sorts a List in place, using either the elements' natural ordering (Comparable) or a Comparator you provide. Collections.reverse() reverses a list's order in place. Collections.max() and Collections.min() find the largest/smallest element without sorting the whole thing.

sort(), reverse(), max(), min()

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

List<Integer> numbers = new ArrayList<>(List.of(5, 2, 8, 1, 9, 3));

Collections.sort(numbers);
System.out.println(numbers);

Collections.reverse(numbers);
System.out.println(numbers);

System.out.println(Collections.max(numbers));
System.out.println(Collections.min(numbers));
[1, 2, 3, 5, 8, 9]
[9, 8, 5, 3, 2, 1]
9
1
unmodifiableList() — Read-Only Views
Collections.unmodifiableList() (and its Set/Map/Collection equivalents) wraps an existing list in a read-only view. The wrapper doesn't copy anything — it's a thin shield in front of the original list, so changes to the underlying list still show through the view, but any attempt to modify the view itself throws UnsupportedOperationException.

unmodifiableList() protects against accidental mutation

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

List<String> mutable = new ArrayList<>(List.of("a", "b", "c"));
List<String> readOnlyView = Collections.unmodifiableList(mutable);

System.out.println(readOnlyView);

try {
    readOnlyView.add("d"); // throws UnsupportedOperationException
} catch (UnsupportedOperationException e) {
    System.out.println("Blocked: " + e.getClass().getSimpleName());
}
[a, b, c]
Blocked: UnsupportedOperationException
It's a view, not an immutable copy
Because unmodifiableList() wraps the original list rather than copying it, changes made directly to mutable (the underlying list) will still be visible through readOnlyView. For a true, independent immutable snapshot, copy the elements first — for example with List.copyOf(mutable).
synchronizedList() — A Teaser
Collections.synchronizedList() (and its Set/Map/Collection equivalents) wraps a collection so that every individual method call is internally synchronized, making it safe for multiple threads to call methods on it without external locking. It's a useful stopgap, but iterating a synchronized collection still requires manually wrapping the loop in a synchronized block on the collection itself. The Threads and Synchronization pages cover this in depth — for now, just know it exists.

Wrapping a list for thread-safe access

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

List<String> threadSafeList =
        Collections.synchronizedList(new ArrayList<>());

threadSafeList.add("safe to call from multiple threads");
System.out.println(threadSafeList);
[safe to call from multiple threads]
emptyList() and singletonList()
For the common cases of "a list with nothing in it" and "a list with exactly one thing in it", Collections.emptyList() and Collections.singletonList() return highly memory-efficient, immutable list instances without the overhead of constructing an ArrayList for something so small.

emptyList() and singletonList()

Java
import java.util.Collections;
import java.util.List;

List<String> nothing = Collections.emptyList();
List<String> justOne = Collections.singletonList("only-value");

System.out.println(nothing);
System.out.println(justOne);
[]
[only-value]
Tip
In modern Java, List.of() is generally preferred over emptyList()/singletonList() for constructing small immutable lists — it reads more naturally and scales up to any number of elements without switching methods.

Method

Purpose

Collections.sort(list)

Sort in place (natural order or Comparator)

Collections.reverse(list)

Reverse the order in place

Collections.max()/min()

Find the largest/smallest element

Collections.unmodifiableList()

Read-only view over an existing list

Collections.synchronizedList()

Thread-safe wrapper around a list

Collections.emptyList()/singletonList()

Efficient tiny immutable lists

  • Collections (class) is a utility toolbox; Collection (interface) is the root of the hierarchy

  • sort(), reverse(), max(), and min() are the everyday workhorses

  • unmodifiableList() returns a read-only view, not a copy

  • synchronizedList() adds thread-safe method calls, foreshadowing the concurrency pages

  • emptyList()/singletonList() avoid overhead for trivially small lists