Java Collections unmodifiableCollection() Method

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

Definition

In Java, Collections.unmodifiableCollection(c) returns a read-only view of the collection c. You can read, loop, and search through the view, but every method that would change it throws UnsupportedOperationException.

It is the most general of the unmodifiable* wrappers: it works for any Collection, such as an ArrayDeque or a PriorityQueue, when you do not need list, set, or map behavior on the result.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.unmodifiableCollection(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Wrap the collection you want to protect.

Java
private final ArrayDeque<String> messages = new ArrayDeque<>();

public Collection<String> getMessages() {
    return Collections.unmodifiableCollection(messages);
}
CallWhat it does
unmodifiableCollection(c)Read-only view; size, contains, iteration all work.
view.add(x) / view.clear()Throws UnsupportedOperationException.
view.iterator().remove()Also throws; there is no back door.
Collection<Number> v = unmodifiableCollection(ints)Can widen the element type, because nothing can be added.

Available since Java 1.2. The view is not a copy: if the owner changes c, readers of the view see the change immediately.

Library declarations
Java
// java.util.Collections
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)

Parameters

ParameterDescription
cThe collection to expose read-only. Must not be null.

Return Value

Return ValueDescription
Collection<T>A read-only view of c.

Compare with Similar Methods

Pick the wrapper that matches the type you return:

MethodHow it differs
unmodifiableList(l)Keeps index access and list equals.
unmodifiableSet(s)Keeps set equals.
synchronizedCollection(c)Thread-safe but still changeable.

Java Examples

Example 1 — Hand Out a Read-Only View of a Queue

An Inbox keeps messages in an ArrayDeque and lets callers look but not touch.

Java
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;

public class CollectionsUnmodifiableCollectionInbox {
    static class Inbox {
        private final ArrayDeque<String> messages = new ArrayDeque<>();

        void receive(String m) {
            messages.addLast(m);
        }

        Collection<String> view() {
            return Collections.unmodifiableCollection(messages);
        }
    }

    public static void main(String[] args) {
        Inbox inbox = new Inbox();
        inbox.receive("hi");
        inbox.receive("lunch?");
        Collection<String> seen = inbox.view();
        System.out.println(seen.size() + " messages: " + seen);

        try {
            seen.clear();
        } catch (UnsupportedOperationException e) {
            System.out.println("clear: " + e.getClass().getSimpleName());
        }
        inbox.receive("ok");
        System.out.println("after receive: " + seen);
    }
}

How It Works

  1. Private storage. Only Inbox can call addLast on its deque; outsiders get the view from view().
  2. Reader blocked. The caller tries to wipe the inbox with clear() and gets UnsupportedOperationException.
  3. Live window. When the owner receives "ok", the same view object shows three messages; it was never a snapshot.

Example 2 — View a List of Integers as Numbers

The ? extends T parameter lets a List<Integer> be exposed as a Collection<Number>. Removal through the iterator or removeIf still fails.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CollectionsUnmodifiableCollectionWiden {
    public static void main(String[] args) {
        List<Integer> ints = new ArrayList<>(Arrays.asList(4, 8, 15));
        Collection<Number> nums = Collections.unmodifiableCollection(ints);

        double total = 0;
        for (Number n : nums) {
            total += n.doubleValue();
        }
        System.out.println(nums + " total " + total);

        Iterator<Number> it = nums.iterator();
        it.next();
        try {
            it.remove();
        } catch (UnsupportedOperationException e) {
            System.out.println("iterator.remove: " + e.getClass().getSimpleName());
        }
        try {
            nums.removeIf(n -> n.intValue() > 5);
        } catch (UnsupportedOperationException e) {
            System.out.println("removeIf: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Safe widening. A plain List<Integer> cannot be assigned to Collection<Number>, but a read-only view can, since nobody can add a Double to it.
  2. Reading works. The loop sums 4 + 8 + 15 as doubles: 27.0.
  3. Every removal path blocked. Both the iterator's remove() and Java 8's removeIf throw UnsupportedOperationException.

Example 3 — Why the View Is Not equal to Its List

Compare an unmodifiableCollection view and an unmodifiableList view against the same list.

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

public class CollectionsUnmodifiableCollectionEquals {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
        Collection<String> plain = Collections.unmodifiableCollection(list);
        List<String> listView = Collections.unmodifiableList(list);

        System.out.println("collection view equals list? " + plain.equals(list));
        System.out.println("collection view equals itself? " + plain.equals(plain));
        System.out.println("list view equals list? " + listView.equals(list));
    }
}

How It Works

  1. Identity only. A general Collection has no agreed meaning for equals, so this view keeps Object.equals: it is not equal to the list.
  2. Itself. The view is equal only to the very same object.
  3. Use the typed wrapper. unmodifiableList follows List.equals, so it compares element by element and returns true.

Frequently Asked Questions

It returns a read-only view of a Collection. Reading works; any change throws UnsupportedOperationException.
No. It is a view, so changes made to the original collection show through it.
unmodifiableCollection does not pass equals and hashCode to the backing collection; it uses object identity. Use unmodifiableList or unmodifiableSet when equality matters.
When the backing object is not a List, Set, or Map, for example a Deque or PriorityQueue, or when you only want to promise a general Collection.
Wrap a copy: Collections.unmodifiableCollection(new ArrayList<>(c)). Nobody else holds a reference to that copy.

Next: unmodifiableList()

Wrap a List in a read-only view that keeps index access.

unmodifiableList() tutorial →

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.

14 people found this page helpful