Skip to content
Tutorials Center
My learning

Topics

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).

Java
List<Integer> nums = new ArrayList<>(List.of(3, 1, 2));
Collections.sort(nums);

Try it

Java A tour of common methods

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.

Java Wrappers return views

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 unmodifiable and synchronized methods wrap the original and share its data. Changes to the backing collection show through. For a true immutable snapshot use List.copyOf (Java 10+).
  • Modern replacements: for immutable literals prefer List.of, Set.of and Map.of (Java 9+) over Collections.emptyList and friends, although both are fine.
  • sort needs a mutable list: sorting List.of(...) throws UnsupportedOperationException.
  • binarySearch requires sorted input in the same order as the comparator used. On unsorted data the result is undefined.
  • Compare with Arrays: Collections handles collections; Arrays handles arrays.
  • Synchronized wrappers still require manual locking while iterating.
  • Prefer concurrent collections (ConcurrentHashMap, CopyOnWriteArrayList) over synchronized wrappers in new code.