Java Collections Class

Beginner
8 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition and Usage

In Java, java.util.Collections (since Java 1.2) is a utility class: a toolbox of static methods that work on collections you already have. It is final, its only constructor is private, and every member is static, so you never write new Collections(). You just call Collections.sort(list).

The methods fall into two families. Algorithms change or inspect a collection, such as sort(), shuffle(), binarySearch(), and max(). Factories and wrappers return a new collection object, such as emptyList(), unmodifiableList(), and synchronizedList(). The Collections Methods index lists every guide.

Errors to expect.

Most methods throw NullPointerException for a null collection, and UnsupportedOperationException when they try to change a collection that cannot be changed (such as one from emptyList() or Arrays.asList when it would need to grow).

Java Syntax

Import the class, then call its static methods on the class name:

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

List<String> names = new ArrayList<>();
Collections.addAll(names, "Cy", "Ana", "Ben");     // [Cy, Ana, Ben]
Collections.sort(names);                           // [Ana, Ben, Cy]
List<String> none = Collections.emptyList();       // [] (read-only)

Collections vs Collection vs the Collections Framework

Three names that sound alike but mean different things:

NameWhat it isExample
CollectionAn interface: the root type of List, Set, and QueueCollection<String> c = new ArrayList<>();
CollectionsA final class of static helper methodsCollections.sort(list);
Collections FrameworkThe whole family of collection interfaces and classes in java.utilList, Set, Map, ArrayList, TreeMap, ...

List, Set, and Map: Which Methods Fit?

Some methods need a List (they work by position); others accept any collection or a map:

TypeIn shortTypical Collections methods
ListOrdered, indexed, duplicates allowed (ArrayList, LinkedList)sort, rotate, swap, copy
SetNo duplicates (HashSet, LinkedHashSet, TreeSet)emptySet, singleton, newSetFromMap
MapKey-value pairs (HashMap, LinkedHashMap, TreeMap)emptyMap, singletonMap, unmodifiableMap
Any CollectionLists, sets, queues, dequesmax, min, frequency, disjoint, addAll

Wrapper Views: Same Data, Different Rules

The unmodifiable*, synchronized*, and checked* methods do not copy anything. They return a wrapper (a view) that forwards every call to your original collection and adds one rule on top. Changes to the original show through the view.

WrapperRule it addsBreaking the rule
unmodifiable*Reads only; no changes through the viewUnsupportedOperationException
synchronized*Each call locks one shared object, for use from several threadsLoops still need synchronized (view) { ... }
checked*Every inserted element must have the stated typeClassCastException at the moment of insertion

A view is not an immutable copy. For lists that nobody can ever change, use Collections.emptyList(), singletonList(), or (Java 9+) List.of(...) and List.copyOf(...) (Java 10+).

Java Examples

Example 2 — Hand Out a Read-Only View

A class keeps its own team list but lets other code look at it only through an unmodifiable view.

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

public class CollectionsHubView {
    public static void main(String[] args) {
        List<String> team = new ArrayList<>();
        team.add("Ana");
        team.add("Ben");
        List<String> readOnly = Collections.unmodifiableList(team);

        try {
            readOnly.add("Eve");
        } catch (UnsupportedOperationException e) {
            System.out.println("Blocked: " + e.getClass().getSimpleName());
        }

        team.add("Cy");
        System.out.println("View now shows " + readOnly + " (size " + readOnly.size() + ")");
    }
}

How It Works

  1. The owner's list. team is an ordinary ArrayList holding Ana and Ben.
  2. Wrap, do not copy. unmodifiableList(team) returns a thin wrapper object that forwards reads to team.
  3. Writes are refused. Calling add on the wrapper throws UnsupportedOperationException, so outside code cannot change the team.
  4. Still live. The owner adds Cy to the original list, and the wrapper immediately reports three names, because it never had its own copy.

Example 3 — Build a List, Count Values, and Replace Them

Start with three repeated votes, add more in one call, count each answer, then rename one answer everywhere.

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

public class CollectionsHubVotes {
    public static void main(String[] args) {
        List<String> votes = new ArrayList<>(Collections.nCopies(3, "yes"));
        Collections.addAll(votes, "no", "yes", "no");
        System.out.println(votes);

        System.out.println("yes=" + Collections.frequency(votes, "yes")
                + " no=" + Collections.frequency(votes, "no"));

        boolean changed = Collections.replaceAll(votes, "no", "maybe");
        System.out.println(changed + " " + votes);
    }
}

How It Works

  1. Repeated start. nCopies(3, "yes") is a fixed read-only list, so it is passed to new ArrayList<>(...) to get a list that can grow.
  2. Many adds in one line. Collections.addAll appends the three extra votes in the order they are written.
  3. Counting. frequency compares every element with equals: four votes say yes and two say no.
  4. Rename everywhere. replaceAll(votes, "no", "maybe") swaps both "no" entries and returns true because at least one element changed.

Next: Java Collections Methods by Group

Full list with short descriptions: Java Collections Methods index.

Frequently Asked Questions

java.util.Collections is a utility class with only static methods. They work on collections you already have: sorting and searching lists, finding the maximum or minimum, counting elements, and creating empty, single-element, read-only, synchronized, or type-checked collections.
Collection is an interface: the root type for List, Set, and Queue. Collections is a final helper class with static methods that accept those collections. You implement or use Collection; you call methods on Collections.
No. The Collections Framework is the whole family of interfaces and classes (Collection, List, Set, Map, ArrayList, HashMap, and so on). The Collections class is one small part of it: a toolbox of static helper methods.
No. It returns a read-only view of the same list. You cannot change the list through the view, but changes made to the original list are visible through it. Copy the list first, or use List.copyOf in Java 10+, if you need a snapshot.
No. Its only constructor is private, and the class is final. All its methods and constants (such as EMPTY_LIST) are static.

Browse every Collections method

Open the methods index for categorized guides with examples and FAQs.

Collections Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful