Java Collections Methods

About This Index
This page is the Java Collections Methods index: every static method of the java.util.Collections utility class, each with a full guide covering syntax, parameters, return value, exceptions, three runnable examples, and FAQs. Every example is a complete Java class with fixed data, so the output shown is the output you get.
Learn what the Collections class is on the Collections hub. Use this page when you need a specific method such as sort(), unmodifiableList(), or frequency(). All examples compile on Java 8 and later.
Collection (no s) is the interface that List, Set, and Queue extend. Collections (with an s) is a separate helper class full of static methods that take those collections as arguments.
How to Call a Collections Method
Every method here is static: import the class once, then call the method on the class name and pass your collection:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
List<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 2));
Collections.sort(nums); // nums = [1, 2, 3]
int biggest = Collections.max(nums); // 3
List<Integer> view = Collections.unmodifiableList(nums); // read-only view Java Collections Methods Table
All method guides in this Java tutorial series (60 tutorials), grouped by purpose.
Sort and reorder a list
6 methodsChange the order of the elements of a List in place: sort them, reverse them, rotate or swap them, or mix them randomly.
| Method | Description | Tutorial |
|---|---|---|
sort() | Sort a list in natural order or with a Comparator. | Try it |
reverse() | Reverse the order of the elements in a list. | Try it |
reverseOrder() | Get a Comparator that sorts in reverse (descending) order. | Try it |
shuffle() | Put the elements of a list in a random order. | Try it |
swap() | Exchange the elements at two positions in a list. | Try it |
rotate() | Shift every element of a list by a distance, wrapping around. | Try it |
Fill, copy, and replace
5 methodsOverwrite list elements, copy one list into another, build a list of repeated values, or add many elements in one call.
| Method | Description | Tutorial |
|---|---|---|
fill() | Replace every element of a list with one value. | Try it |
copy() | Copy one list into the start of another list. | Try it |
nCopies() | Create an immutable list holding n copies of one value. | Try it |
replaceAll() | Replace every occurrence of one value with another. | Try it |
addAll() | Add several elements to a collection in one call. | Try it |
Search and count
7 methodsFind an element or a run of elements, get the largest or smallest element, count matches, or check for common elements.
| Method | Description | Tutorial |
|---|---|---|
binarySearch() | Find an element in a sorted list quickly. | Try it |
indexOfSubList() | Find where a sequence of elements first appears in a list. | Try it |
lastIndexOfSubList() | Find where a sequence of elements last appears in a list. | Try it |
max() | Return the largest element of a collection. | Try it |
min() | Return the smallest element of a collection. | Try it |
frequency() | Count how many elements equal a given value. | Try it |
disjoint() | Check whether two collections have no element in common. | Try it |
Empty collections and iterators
10 methodsGet a ready-made, immutable empty collection, map, or iterator instead of returning null.
| Method | Description | Tutorial |
|---|---|---|
emptyList() | Get an immutable, shared empty List. | Try it |
emptySet() | Get an immutable, shared empty Set. | Try it |
emptyMap() | Get an immutable, shared empty Map. | Try it |
emptySortedSet() | Get an immutable empty SortedSet (Java 8+). | Try it |
emptySortedMap() | Get an immutable empty SortedMap (Java 8+). | Try it |
emptyNavigableSet() | Get an immutable empty NavigableSet (Java 8+). | Try it |
emptyNavigableMap() | Get an immutable empty NavigableMap (Java 8+). | Try it |
emptyIterator() | Get an Iterator that has no elements (Java 7+). | Try it |
emptyListIterator() | Get a ListIterator that has no elements (Java 7+). | Try it |
emptyEnumeration() | Get a legacy Enumeration that has no elements (Java 7+). | Try it |
Single-element collections
3 methodsWrap exactly one element (or one key-value pair) in a small immutable collection.
| Method | Description | Tutorial |
|---|---|---|
singleton() | Create an immutable Set holding exactly one element. | Try it |
singletonList() | Create an immutable List holding exactly one element. | Try it |
singletonMap() | Create an immutable Map holding exactly one key-value pair. | Try it |
Read-only (unmodifiable) views
8 methodsHand out a view that can be read but not changed. Changes made to the original collection still show through the view.
| Method | Description | Tutorial |
|---|---|---|
unmodifiableCollection() | Wrap any Collection in a read-only view. | Try it |
unmodifiableList() | Wrap a List in a read-only view that keeps index access. | Try it |
unmodifiableSet() | Wrap a Set in a read-only view. | Try it |
unmodifiableSortedSet() | Wrap a SortedSet in a read-only view, including its range views. | Try it |
unmodifiableNavigableSet() | Wrap a NavigableSet in a read-only view that still supports ceiling and floor. | Try it |
unmodifiableMap() | Wrap a Map in a read-only view. | Try it |
unmodifiableSortedMap() | Wrap a SortedMap in a read-only view, including its range views. | Try it |
unmodifiableNavigableMap() | Wrap a NavigableMap in a read-only view that still supports floorEntry and ceilingKey. | Try it |
Thread-safe (synchronized) wrappers
8 methodsWrap a collection so every method call locks one shared object. Loops over the wrapper still need a synchronized block.
| Method | Description | Tutorial |
|---|---|---|
synchronizedCollection() | Wrap any Collection so each method call is thread-safe. | Try it |
synchronizedList() | Wrap a List so each method call is thread-safe. | Try it |
synchronizedSet() | Wrap a Set so each method call is thread-safe. | Try it |
synchronizedSortedSet() | Wrap a SortedSet so each call, including range views, is thread-safe. | Try it |
synchronizedNavigableSet() | Wrap a NavigableSet so navigation and pollFirst() are thread-safe. | Try it |
synchronizedMap() | Wrap a Map so each method call is thread-safe. | Try it |
synchronizedSortedMap() | Wrap a SortedMap so each call, including range views, is thread-safe. | Try it |
synchronizedNavigableMap() | Wrap a NavigableMap so floorEntry, pollFirstEntry, and views are thread-safe. | Try it |
Type-safe (checked) views
9 methodsWrap a collection so that adding an element of the wrong type fails at once with ClassCastException.
| Method | Description | Tutorial |
|---|---|---|
checkedCollection() | Wrap a Collection so inserting the wrong element type fails immediately. | Try it |
checkedList() | Wrap a List so add, set, and list-iterator writes reject wrong types. | Try it |
checkedSet() | Wrap a Set so adding the wrong element type fails immediately. | Try it |
checkedSortedSet() | Wrap a SortedSet so wrong-type elements are rejected, including through range views. | Try it |
checkedNavigableSet() | Wrap a NavigableSet so inserts are type-checked while navigation and polling still work. | Try it |
checkedQueue() | Wrap a Queue so offer and add reject wrong element types. | Try it |
checkedMap() | Wrap a Map so wrong key or value types are rejected on insert. | Try it |
checkedSortedMap() | Wrap a SortedMap so wrong key or value types are rejected, including in sub-maps. | Try it |
checkedNavigableMap() | Wrap a NavigableMap so inserts are type-checked while floorEntry and polling still work. | Try it |
Adapters and legacy bridges
4 methodsTurn one kind of collection into another: a Deque into a stack-like Queue, a Map into a Set, or a collection into an old-style Enumeration and back.
| Method | Description | Tutorial |
|---|---|---|
asLifoQueue() | View a Deque as a last-in-first-out Queue (a stack behind the Queue interface). | Try it |
newSetFromMap() | Build a Set backed by any Map, such as a ConcurrentHashMap. | Try it |
enumeration() | Get an old-style Enumeration over any modern collection. | Try it |
list() | Copy the elements of an Enumeration into a new ArrayList. | Try it |
Frequently Asked Questions
Start with sort()
Put a list in order with one call, then sort it your own way with a Comparator.
12 people found this page helpful
