Java Collections synchronizedCollection() Method

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

Definition

In Java, Collections.synchronizedCollection(c) returns a wrapper whose methods each run while holding a lock, so several threads can call add, remove, or size on it without corrupting the data.

Each single call is safe, but a sequence of calls is not automatically atomic. Iterating, or "check then add", must be wrapped in synchronized (wrapper) { ... } yourself.

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

Syntax

Wrap a new collection and share only the wrapper.

Java
Collection<Integer> hits = Collections.synchronizedCollection(new ArrayList<Integer>());
hits.add(1);                         // locked for you
synchronized (hits) {                // lock manually to iterate
    for (int h : hits) { /* ... */ }
}
CallWhat it does
hits.add(x) / remove(x) / size()Each call is atomic.
hits.forEach(...) / removeIf(...)Java 8 bulk methods also lock for you.
for (x : hits) under synchronized (hits)Iteration needs a manual lock on the wrapper.
synchronized (hits) { if (!contains) add }Compound actions need a manual lock.

Available since Java 1.2. Like unmodifiableCollection, the wrapper does not pass equals/hashCode through to the backing collection; it uses object identity.

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

Parameters

ParameterDescription
cThe collection to make thread-safe. Do not use it directly afterwards.

Return Value

Return ValueDescription
Collection<T>A synchronized (thread-safe) view of c.

Compare with Similar Methods

Related wrappers and alternatives:

MethodHow it differs
synchronizedList(l)Thread-safe list with index access.
unmodifiableCollection(c)Read-only; safe to share if nobody writes.
java.util.concurrent classesLock-free or finer-grained structures for heavy concurrency.

Java Examples

Example 1 — Two Threads Adding at Once

Both threads add 1000 numbers to the same synchronized collection. No add is lost.

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

public class CollectionsSynchronizedCollectionThreads {
    public static void main(String[] args) throws InterruptedException {
        Collection<Integer> hits = Collections.synchronizedCollection(new ArrayList<Integer>());
        Runnable work = () -> {
            for (int i = 0; i < 1000; i++) {
                hits.add(i);
            }
        };
        Thread a = new Thread(work);
        Thread b = new Thread(work);
        a.start();
        b.start();
        a.join();
        b.join();
        System.out.println("size: " + hits.size() + ", contains 999? " + hits.contains(999));
    }
}

How It Works

  1. Shared target. Both threads run the same task against the one wrapper object.
  2. Locked adds. Every add holds the wrapper's lock, so the underlying ArrayList never sees two writes at once. A plain ArrayList here could lose elements or throw.
  3. Wait, then read. join() waits for both threads, so the count is reliably 2 × 1000 = 2000.

Example 2 — Iterate Under the Lock; forEach Locks for You

Join log lines with a for-each loop inside synchronized, then let Java 8's forEach handle the lock.

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

public class CollectionsSynchronizedCollectionIterate {
    public static void main(String[] args) {
        Collection<String> log = Collections.synchronizedCollection(new ArrayList<String>());
        log.add("start");
        log.add("load");
        log.add("ready");

        StringBuilder sb = new StringBuilder();
        synchronized (log) {
            for (String line : log) {
                sb.append(line).append(" > ");
            }
        }
        System.out.println(sb + "done");

        int[] letters = {0};
        log.forEach(s -> letters[0] += s.length());
        System.out.println("letters: " + letters[0]);
    }
}

How It Works

  1. Manual lock. A for-each loop makes many calls on the iterator, so the code holds synchronized (log) for the whole loop.
  2. Lock the wrapper. The lock object must be the wrapper itself, because that is what its methods synchronize on.
  3. Built-in locking. forEach is overridden to lock internally, so no block is needed; 5 + 4 + 5 = 14 letters.

Example 3 — Add Only If Absent, from Two Threads

contains followed by add is two calls. Without a lock, both threads could pass the check and add a duplicate.

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

public class CollectionsSynchronizedCollectionCheckAct {
    static void addIfAbsent(Collection<String> c, String x) {
        synchronized (c) {
            if (!c.contains(x)) {
                c.add(x);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Collection<String> users = Collections.synchronizedCollection(new ArrayList<String>());
        Runnable signUps = () -> {
            for (int i = 0; i < 100; i++) {
                addIfAbsent(users, "u" + i);
            }
        };
        Thread a = new Thread(signUps);
        Thread b = new Thread(signUps);
        a.start();
        b.start();
        a.join();
        b.join();
        System.out.println("unique users: " + users.size());
    }
}

How It Works

  1. Two steps, one lock. addIfAbsent wraps the check and the add in synchronized (c), so no other thread can slip in between.
  2. Same names from both threads. Both threads try to register u0 to u99.
  3. No duplicates. Each name is added exactly once, so the size is 100. (The order of names may vary between runs, which is why it is not printed.)

Frequently Asked Questions

It returns a wrapper whose methods each acquire a lock, making single calls thread-safe.
Yes. Wrap the whole loop in synchronized (wrapper) { ... }; otherwise another thread may change the collection mid-loop.
No. Two separate calls can interleave with another thread. Put both inside one synchronized (wrapper) block.
The wrapper returned by synchronizedCollection, not the original collection.
Yes. In Java 8+ the wrapper overrides them to run while holding its lock.

Next: synchronizedList()

Wrap a List so each method call is thread-safe.

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

15 people found this page helpful