Collections Utility Class
Use the java.util.Collections class for sorting, searching, shuffling, and creating safe collection wrappers.
Overview
Key Points
- •Collections.sort(list) sorts in natural order; sort(list, comparator) uses a custom order
- •Collections.reverse(list) reverses in place
- •Collections.shuffle(list) randomises order
- •Collections.min(collection) and max(collection) find extremes
- •Collections.frequency(collection, element) counts occurrences
- •Collections.nCopies(n, element) creates an immutable list with n copies
- •Collections.unmodifiableList(list) wraps a list to prevent modification
- •Collections.singletonList(element) creates a one-element immutable list
Examples
Sorting, reversing, and shuffling
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(5, 2, 8, 1, 9, 3));
Collections.sort(nums);
System.out.println("Sorted: " + nums); // [1, 2, 3, 5, 8, 9]
Collections.reverse(nums);
System.out.println("Reversed: " + nums); // [9, 8, 5, 3, 2, 1]
Collections.shuffle(nums);
System.out.println("Shuffled: " + nums); // random order
System.out.println("Min: " + Collections.min(nums));
System.out.println("Max: " + Collections.max(nums));
}
}sort, reverse, and shuffle modify the list in place. They do not return a new list.
Binary search and frequency
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("Alice","Bob","Carol","Dave","Eve"));
// Must be sorted before binarySearch
Collections.sort(names);
System.out.println("Sorted: " + names);
int idx = Collections.binarySearch(names, "Carol");
System.out.println("Carol at index: " + idx); // 1
int missing = Collections.binarySearch(names, "Zara");
System.out.println("Zara search: " + missing); // negative = not found
List<String> words = List.of("a","b","a","c","a","b");
System.out.println("Frequency of 'a': " + Collections.frequency(words, "a")); // 3
}
}binarySearch requires the list to be sorted. It returns a negative number when the element is not found.
Unmodifiable and singleton wrappers
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> mutable = new ArrayList<>(List.of("A", "B", "C"));
List<String> readonly = Collections.unmodifiableList(mutable);
System.out.println(readonly); // [A, B, C]
try {
readonly.add("D"); // throws UnsupportedOperationException
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify read-only list.");
}
// nCopies: useful for initialising lists
List<Integer> zeros = new ArrayList<>(Collections.nCopies(5, 0));
System.out.println("Zeros: " + zeros); // [0, 0, 0, 0, 0]
zeros.set(2, 99);
System.out.println("Modified: " + zeros); // [0, 0, 99, 0, 0]
}
}unmodifiableList wraps the original list. Changes to the original are still reflected in the wrapper. For a truly independent immutable copy, use List.copyOf(list).
Common Mistakes
- Running binarySearch on an unsorted list. The result is undefined and may be incorrect even if the element is present
- Expecting unmodifiableList to prevent changes when the underlying list changes. The wrapper just blocks direct modification through itself
- Confusing java.util.Collections (utility class) with the java.util.Collection interface
Note
For a custom sort key, use list.sort(Comparator.comparing(Person::getName)) instead of implementing Comparable. This is often cleaner for one-off sorts.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4