Skip to content
Dev Dump

Collections Utility Class

java.util.Collections is a utility class with static methods that operate on or return collections. It’s the Swiss army knife for collection manipulation — sorting, searching, wrapping, and creating specialized collection instances.

Don’t confuse Collections (utility class) with Collection (interface).

List<Integer> list = new ArrayList<>(List.of(3, 1, 4, 1, 5, 9));
Collections.sort(list); // [1, 1, 3, 4, 5, 9]
Collections.sort(list, Comparator.reverseOrder()); // [9, 5, 4, 3, 1, 1]
Collections.reverse(list); // reverses in-place
Collections.shuffle(list); // random order
Collections.swap(list, 0, 2); // swap indices 0 and 2
Collections.rotate(list, 2); // rotates right by 2 positions
List<Integer> sorted = List.of(1, 3, 5, 7, 9);
int idx = Collections.binarySearch(sorted, 5); // 2 (index of 5)
int miss = Collections.binarySearch(sorted, 4); // negative (not found)

binarySearch requires the list to be sorted. Returns -(insertion point) - 1 when not found.

List<Integer> nums = List.of(3, 1, 4, 1, 5);
Collections.min(nums); // 1
Collections.max(nums); // 5
Collections.frequency(nums, 1); // 2
Collections.disjoint(list1, list2); // true if no common elements

Return read-only views of a collection. Mutations throw UnsupportedOperationException. The view reflects changes to the original — it’s not a copy.

List<String> mutable = new ArrayList<>(List.of("a", "b", "c"));
List<String> readOnly = Collections.unmodifiableList(mutable);
readOnly.add("d"); // throws UnsupportedOperationException
mutable.add("d"); // works — and readOnly now shows "d" too

Available for all collection types:

  • unmodifiableList(), unmodifiableSet(), unmodifiableMap()
  • unmodifiableSortedSet(), unmodifiableSortedMap()
  • unmodifiableNavigableSet(), unmodifiableNavigableMap()

Note: For truly immutable collections, prefer List.of(), Set.of(), Map.of() (Java 9+). These are not views — they’re independent and fully immutable.

Wrap a collection so every method is synchronized. Quick fix for thread safety, but prefer java.util.concurrent collections for better performance.

List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Map<String, Integer> syncMap = Collections.synchronizedMap(new HashMap<>());
Set<String> syncSet = Collections.synchronizedSet(new HashSet<>());

Iteration still requires manual synchronization:

synchronized (syncList) {
for (String item : syncList) {
process(item);
}
}

Enforce type safety at runtime — useful when working with raw types or untrusted code. Throws ClassCastException immediately on a type violation instead of failing later.

List<String> checked = Collections.checkedList(new ArrayList<>(), String.class);
// checked.add(123); // throws ClassCastException at insertion time

Immutable, shared instances. Cheaper than creating new ArrayList<>() when you just need an empty collection to return.

List<String> empty = Collections.emptyList();
Set<String> emptySet = Collections.emptySet();
Map<String, Integer> emptyMap = Collections.emptyMap();

Immutable collections with exactly one element. Useful for APIs that require a collection but you only have one item.

List<String> single = Collections.singletonList("only");
Set<String> singleSet = Collections.singleton("only");
Map<String, Integer> singleMap = Collections.singletonMap("key", 42);
List<String> list = new ArrayList<>(List.of("a", "b", "c"));
Collections.fill(list, "x"); // [x, x, x]
Collections.replaceAll(list, "x", "y"); // [y, y, y]
Collections.nCopies(5, "hello"); // immutable [hello, hello, hello, hello, hello]
MethodWhat it does
sort, reverse, shuffle, rotateReorder elements
binarySearchFind element in sorted list
min, max, frequencyAggregate operations
unmodifiableXxxRead-only view
synchronizedXxxThread-safe wrapper
checkedXxxRuntime type enforcement
emptyXxxShared immutable empty instance
singletonXxxImmutable single-element collection
nCopies, fill, replaceAllBulk create/modify