11.5 The Collections Utility Class
java.util.Collections is a collection of static utility methods for working with collections. It provides sorting, searching, reversing, and creating immutable collections.
1. Sorting and Order Manipulation
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9, 3));
Collections.sort(numbers); // Ascending
Collections.sort(numbers, Comparator.reverseOrder()); // Descending
Collections.reverse(numbers); // Reverse
Collections.shuffle(numbers); // Random shuffle
Collections.shuffle(numbers, new Random(42)); // With seed (reproducible)
List<String> letters = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
Collections.rotate(letters, -2); // Rotate left by 2
System.out.println(letters); // [C, D, E, A, B]
Collections.swap(letters, 0, 4); // Swap elements at index 0 and 4
2. Search and Statistics
List<Integer> sorted = Arrays.asList(1, 3, 5, 7, 9, 11);
int idx = Collections.binarySearch(sorted, 7); // 3 (index)
System.out.println(Collections.min(sorted)); // 1
System.out.println(Collections.max(sorted)); // 11
List<String> fruits = Arrays.asList("apple", "banana", "apple", "cherry", "apple");
System.out.println(Collections.frequency(fruits, "apple")); // 3
3. Immutable Collections
// Wrap existing collection as unmodifiable
List<String> mutable = new ArrayList<>(Arrays.asList("A", "B", "C"));
List<String> immutable = Collections.unmodifiableList(mutable);
// immutable.add("D"); // ❌ UnsupportedOperationException
// Factory methods (Java 9+) - truly immutable
List<String> list = List.of("Alice", "Bob", "Charlie");
Set<Integer> set = Set.of(1, 2, 3, 4, 5);
Map<String, Integer> map = Map.of("Alice", 95, "Bob", 87, "Carol", 92);
// More than 10 entries
Map<String, Integer> bigMap = Map.ofEntries(
Map.entry("A", 1), Map.entry("B", 2), Map.entry("C", 3)
);
4. Special Collections
List<String> emptyList = Collections.emptyList(); // Empty immutable list
Set<String> emptySet = Collections.emptySet();
List<String> singleList = Collections.singletonList("only");
List<Integer> zeros = Collections.nCopies(5, 0); // [0, 0, 0, 0, 0]
5. Collections with Lambdas (Java 8+)
List<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "Dave"));
names.removeIf(name -> name.length() < 4); // Remove short names
names.replaceAll(String::toUpperCase); // Transform all elements
names.sort(Comparator.comparingInt(String::length)); // Sort by length
// Map utilities (Java 8+)
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.getOrDefault("Carol", 0); // 0 if absent
scores.putIfAbsent("Alice", 100); // Ignored (already exists)
scores.computeIfAbsent("Dave", k -> 75); // Add Dave with 75
scores.merge("Alice", 5, Integer::sum); // Add 5 to Alice's score
scores.forEach((name, score) ->
System.out.println(name + ": " + score));
Pro Tip
Immutable collections: key difference:
List.of(),Map.of()(Java 9+): Truly immutable — throws exception on modificationCollections.unmodifiableList(): Just a wrapper — changes to the original list are reflected
List<String> original = new ArrayList<>(List.of("A", "B"));
List<String> unmod = Collections.unmodifiableList(original);
original.add("C");
System.out.println(unmod); // [A, B, C] ← changed!
// Truly immutable copy
List<String> trulyImmutable = List.copyOf(original); // Java 10+
Return Collections.emptyList() instead of null from methods — prevents NPE and simplifies calling code.