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).
Sorting and Ordering
Section titled “Sorting and Ordering”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-placeCollections.shuffle(list); // random orderCollections.swap(list, 0, 2); // swap indices 0 and 2Collections.rotate(list, 2); // rotates right by 2 positionsSearching
Section titled “Searching”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.
Min, Max, Frequency
Section titled “Min, Max, Frequency”List<Integer> nums = List.of(3, 1, 4, 1, 5);
Collections.min(nums); // 1Collections.max(nums); // 5Collections.frequency(nums, 1); // 2Collections.disjoint(list1, list2); // true if no common elementsUnmodifiable Wrappers
Section titled “Unmodifiable Wrappers”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 UnsupportedOperationExceptionmutable.add("d"); // works — and readOnly now shows "d" tooAvailable 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.
Synchronized Wrappers
Section titled “Synchronized Wrappers”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); }}Checked Wrappers
Section titled “Checked Wrappers”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 timeEmpty Collections
Section titled “Empty Collections”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();Singleton Collections
Section titled “Singleton Collections”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);Fill and Replace
Section titled “Fill and Replace”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]Summary
Section titled “Summary”| Method | What it does |
|---|---|
sort, reverse, shuffle, rotate | Reorder elements |
binarySearch | Find element in sorted list |
min, max, frequency | Aggregate operations |
unmodifiableXxx | Read-only view |
synchronizedXxx | Thread-safe wrapper |
checkedXxx | Runtime type enforcement |
emptyXxx | Shared immutable empty instance |
singletonXxx | Immutable single-element collection |
nCopies, fill, replaceAll | Bulk create/modify |