Java Reference ยท Collections
The Collections class
Overview of java.util.Collections: sorting, searching, reversing, wrappers, empty and singleton collections, and the synchronized and unmodifiable views.
Collections (with an s) is a utility class full of static methods that operate on or return collections. It is not the same as the Collection interface. You never create an instance; you call methods such as Collections.sort(list) or Collections.unmodifiableList(list).
The methods fall into a few groups: algorithms that work on lists (sorting, searching, shuffling, reversing), queries over any collection (min, max, frequency), factory methods for empty or single-element collections, and wrappers that return a view with different behavior (read-only, synchronized, type-checked).
List<Integer> nums = new ArrayList<>(List.of(3, 1, 2)); Collections.sort(nums);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
All members are static. The most useful ones:
| Method | Description |
|---|---|
sort(List<T>), sort(List<T>, Comparator) |
Stable in-place sort. |
binarySearch(List, key) |
Index of key in a sorted list, or -(insertion point) - 1. |
reverse, shuffle, swap, rotate, fill |
In-place list rearrangements. |
max, min |
Largest and smallest by natural order or a comparator. |
frequency(Collection, Object) |
How many elements equal the object. |
disjoint(Collection, Collection) |
True if they share no element. |
addAll(Collection, T...) |
Adds all listed elements. |
nCopies(int, T) |
Immutable list of n references to one object. |
emptyList(), emptySet(), emptyMap(), singletonList(x), singleton(x) |
Immutable ready-made collections. |
unmodifiableList/Set/Map/Collection(c) |
Read-only views. |
synchronizedList/Set/Map(c) |
Thread-safe wrappers (each call locks). |
reverseOrder(), reverseOrder(cmp) |
A comparator for the opposite order. |
Throws: NoSuchElementException from max/min on an empty collection; UnsupportedOperationException when an algorithm needs to modify an unmodifiable list.
Usage notes
- Views vs copies: the
unmodifiableandsynchronizedmethods wrap the original and share its data. Changes to the backing collection show through. For a true immutable snapshot useList.copyOf(Java 10+). - Modern replacements: for immutable literals prefer
List.of,Set.ofandMap.of(Java 9+) overCollections.emptyListand friends, although both are fine. sortneeds a mutable list: sortingList.of(...)throwsUnsupportedOperationException.binarySearchrequires sorted input in the same order as the comparator used. On unsorted data the result is undefined.- Compare with
Arrays:Collectionshandles collections;Arrayshandles arrays. - Synchronized wrappers still require manual locking while iterating.
- Prefer concurrent collections (
ConcurrentHashMap,CopyOnWriteArrayList) oversynchronizedwrappers in new code.