Codectionary / Developer documentation / Java

Collections Utility Class

java.util.Collections is a utility class full of static helper methods for working with collections - sorting, shuffling, finding the minimum/maximum, reversing order, and creating unmodifiable or synchronized wrapper collections. It's distinct from the Collection interface itself (singular), which is the root interface that List, Set, and Queue all extend.

Syntax

Collections.methodName(collection);

Examples

Sorting and Reversing

Common list-ordering operations.

import java.util.ArrayList;
import java.util.Collections;

ArrayList<Integer> numbers = new ArrayList<>(java.util.List.of(5, 2, 8, 1, 9));

Collections.sort(numbers);
System.out.println(numbers);  // [1, 2, 5, 8, 9]

Collections.reverse(numbers);
System.out.println(numbers);  // [9, 8, 5, 2, 1]

Finding min, max, and frequency

Quickly computing summary statistics over a collection.

import java.util.ArrayList;
import java.util.Collections;

ArrayList<Integer> scores = new ArrayList<>(java.util.List.of(85, 92, 78, 92, 90));

System.out.println(Collections.max(scores));               // 92
System.out.println(Collections.min(scores));                // 78
System.out.println(Collections.frequency(scores, 92));       // 2

Creating Unmodifiable Collections

Wrapping a collection to prevent any further modification, useful for defensive programming.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

ArrayList<String> mutable = new ArrayList<>(java.util.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 - it's read-only");
}

Shuffling and Empty Collections

Randomizing element order, and getting properly typed empty collections.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

ArrayList<Integer> deck = new ArrayList<>(java.util.List.of(1, 2, 3, 4, 5));
Collections.shuffle(deck);
System.out.println(deck);  // random order each run

List<String> empty = Collections.emptyList();
System.out.println(empty.size());  // 0

Best practices

  • Use Collections.unmodifiableList()/Set()/Map() to safely expose internal collections from a class without allowing external code to modify them
  • Use Collections.sort() with a custom Comparator for sorting by criteria other than natural ordering
  • Use Collections.emptyList()/emptySet()/emptyMap() instead of creating a new empty ArrayList/HashSet/HashMap when you just need an immutable, empty placeholder
  • Don't confuse Collections (the utility class, plural) with Collection (the root interface, singular) - they serve very different purposes

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

ArrayList
ArrayList is a resizable array implementation of the List interface, part of java.util. Unlike a plain array, an ArrayList automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and iterating. Since generics require object types, an ArrayList of primitives (like int) actually stores their wrapper class (Integer) via autoboxing.
LinkedList
LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.
HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).
HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.