68. The Collections Utility Class
java.util.Collections is a utility class of static methods for operating on collections: sorting, shuffling, searching, wrapping, and creating singleton/empty/unmodifiable views.
Explanation
Key static methods: sort(list) and sort(list, comparator) sort in-place using merge sort (stable, O(n log n)). binarySearch(list, key) requires a sorted list. shuffle(list) randomises. reverse(list) reverses in-place. fill(list, obj) replaces all elements.
Utility factories: singletonList(obj) returns a 1-element immutable list. emptyList(), emptySet(), emptyMap() return shared immutable empty collections (prefer List.of() etc. in modern code). nCopies(n, obj) returns an immutable list of n copies.
Wrapping methods: unmodifiableList(list) returns a view that throws UnsupportedOperationException on writes (but the underlying list can still be modified). synchronizedList(list) wraps each method with synchronisation. checkedList(list, type) adds runtime type checking.
Statistics: min(coll), max(coll), frequency(coll, obj) (occurrence count), disjoint(c1, c2) (true if no common elements).
Example
import java.util.*;
public class CollectionsUtilityClass {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(3, 1, 4, 1, 5, 9, 2, 6));
// sort and binarySearch
Collections.sort(nums);
System.out.println("Sorted: " + nums);
int idx = Collections.binarySearch(nums, 5);
System.out.println("Index of 5: " + idx); // >= 0
// shuffle (random order)
Collections.shuffle(nums, new Random(42));
System.out.println("Shuffled: " + nums);
// reverse
Collections.reverse(nums);
System.out.println("Reversed: " + nums);
// min, max, frequency
Collections.sort(nums); // re-sort
System.out.println("Min: " + Collections.min(nums));
System.out.println("Max: " + Collections.max(nums));
System.out.println("Frequency of 1: " + Collections.frequency(nums, 1)); // 2
// disjoint
List<Integer> a = List.of(1, 2, 3);
List<Integer> b = List.of(4, 5, 6);
List<Integer> c = List.of(3, 7, 8);
System.out.println("a,b disjoint: " + Collections.disjoint(a, b)); // true
System.out.println("a,c disjoint: " + Collections.disjoint(a, c)); // false (3 common)
// Singleton collections (immutable, single-element)
List<String> single = Collections.singletonList("only");
System.out.println("Singleton: " + single);
// unmodifiableList: view โ original can still be modified
List<Integer> mutable = new ArrayList<>(List.of(1, 2, 3));
List<Integer> readOnly = Collections.unmodifiableList(mutable);
mutable.add(4); // affects readOnly too!
System.out.println("readOnly (4 added): " + readOnly); // [1, 2, 3, 4]
// Prefer List.copyOf() for true independence:
List<Integer> trulyImmutable = List.copyOf(mutable); // snapshot
mutable.add(5);
System.out.println("trulyImmutable: " + trulyImmutable); // [1, 2, 3, 4] (no 5)
}
}Sources
- https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collections.html โ Collections utility class
- https://docs.oracle.com/javase/tutorial/collections/algorithms/index.html โ Algorithms tutorial