java.util · java.base · since Java 1.2
Collections
public final class CollectionsStatic algorithms and wrappers over collections: sort/shuffle/rotate/binarySearch, min/max/frequency, unmodifiable and synchronized views, and empty/singleton factories.
Key methods
static <T> void sort(List<T> l) / sort(List<T> l, Comparator<? super T> c) | In-place stable TimSort; null comparator = natural order (superseded in practice by List.sort, same algorithm). |
static <T> int binarySearch(List<? extends T> l, T key) / binarySearch(l, key, Comparator) | Sorted lists only; negative return = -(insertion point) - 1. |
static void reverse(List<?> l) | In-place order reversal. |
static void shuffle(List<?> l) / shuffle(l, Random) | In-place Fisher–Yates; pass a seeded Random for reproducible shuffles. |
static void swap(List<?> l, int i, int j) | Swap two positions in place. |
static void rotate(List<?> l, int distance) | Circular shift — positive distance moves elements toward higher indices, wrapping around. |
static <T> void fill(List<? super T> l, T obj) | Overwrite every existing element (does not grow the list). |
static <T> void copy(List<? super T> dest, List<? extends T> src) | Overwrite dest[0..src.size()) with src; dest must already be at least as large — throws IndexOutOfBoundsException otherwise. |
static <T> T min/max(Collection<? extends T> c) / min/max(c, Comparator) | Extremes by natural order or comparator — O(n) scan. |
static int frequency(Collection<?> c, Object o) | Occurrence count via equals(). |
static boolean disjoint(Collection<?> a, Collection<?> b) | True if the two collections share no elements. |
static <T> boolean addAll(Collection<? super T> c, T... elements) | Varargs bulk insert — handier than c.addAll(Arrays.asList(...)). |
static <T> boolean replaceAll(List<T> l, T oldVal, T newVal) | Replace every occurrence of oldVal; returns whether anything changed. |
static <T> List<T> nCopies(int n, T o) | Immutable virtual list of n references to the same object — O(1) memory. |
static <T> List<T> emptyList() / <K,V> Map<K,V> emptyMap() / <T> Set<T> emptySet() | Immutable, always-empty singletons — cheaper and clearer than new ArrayList<>() for a constant empty value. |
static <T> List<T> singletonList(T o) / <T> Set<T> singleton(T o) / <K,V> Map<K,V> singletonMap(K,V) | Immutable one-element collections — lighter than List.of(o) pre-Java 9 habits, still idiomatic for a fixed single value. |
static <T> List<T> unmodifiableList(List<? extends T>) / unmodifiableMap/unmodifiableSet/unmodifiableCollection(…) | Read-only VIEW that throws UnsupportedOperationException on mutation — the backing collection can still change and those changes show through. |
static <T> List<T> synchronizedList(List<T>) / synchronizedMap/synchronizedCollection(…) | Wraps every method with a lock on the wrapper — safe for single calls, but iteration still needs a manual synchronized(wrapper) block. |
Example
List<Integer> nums = new ArrayList<>(List.of(5, 3, 9, 1));
Collections.sort(nums); // [1, 3, 5, 9]
int i = Collections.binarySearch(nums, 5); // 2
Collections.reverse(nums); // [9, 5, 3, 1]
List<Integer> readOnly = Collections.unmodifiableList(nums);
nums.set(0, 100); // readOnly[0] is now 100 too — it's a VIEW, not a copy
readOnly.set(0, 1); // throws UnsupportedOperationException