Collections synchronizedSet() method in Java with Examples
Last Updated :
22 Oct, 2025
In Java, the Collections.synchronizedSet() method creates a thread-safe (synchronized) Set backed by a specified set, allowing safe concurrent access by multiple threads.
- Thread Safety: All operations on the returned set are synchronized.
- Iteration: Iterating over the synchronized set must be done inside a synchronized block to avoid ConcurrentModificationException.
Syntax
public static <T> Set<T> synchronizedSet(Set<T> s)
- Parameters: s is the set to be wrapped in a synchronized (thread-safe) set.
- Return Value: Returns a synchronized view of the specified set.
Example 1: Synchronized Set of Strings
Java
import java.util.*;
public class GFG{
public static void main(String[] args){
// Create a HashSet of Strings
Set<String> set = new HashSet<>();
set.add("1");
set.add("2");
set.add("3");
// Print original set
System.out.println("Original Set: " + set);
// Create a synchronized set
Set<String> synSet = Collections.synchronizedSet(set);
// Print synchronized set
System.out.println("Synchronized Set: " + synSet);
}
}
OutputOriginal Set: [1, 2, 3]
Synchronized Set: [1, 2, 3]
Explanation:
- Creates a normal HashSet and wraps it with Collections.synchronizedSet().
- Both original and synchronized sets contain the same elements and are now thread-safe
Example 2: Synchronized Set of Integers
Java
import java.util.*;
public class GFG{
public static void main(String[] args){
// Create a HashSet of Integers
Set<Integer> set = new HashSet<>();
set.add(100);
set.add(200);
set.add(300);
// Print original set
System.out.println("Original Set: " + set);
// Create a synchronized set
Set<Integer> synSet = Collections.synchronizedSet(set);
// Print synchronized set
System.out.println("Synchronized Set: " + synSet);
}
}
OutputOriginal Set: [100, 200, 300]
Synchronized Set: [100, 200, 300]
Explanation:
- Demonstrates creating a synchronized set with integer values.
- Ensures safe access when multiple threads work with the set.
Example 3: Iterating Over a Synchronized Set
Java
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GFG{
public static void main(String[] args){
Set<String> normalSet = new HashSet<>();
normalSet.add("Red");
normalSet.add("Green");
normalSet.add("Blue");
Set<String> syncSet = Collections.synchronizedSet(normalSet);
// Synchronize during iteration
synchronized (syncSet) {
Iterator<String> iterator = syncSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
}
Explanation:
- Iteration over a synchronized set must be done inside a synchronized block.
- Prevents ConcurrentModificationException when accessed by multiple threads.
Example 4: Multi-threaded Access
Java
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class GFG {
public static void main(String[] args){
Set<Integer> set
= Collections.synchronizedSet(new HashSet<>());
Runnable addNumbers = () ->
{
for (int i = 1; i <3; i++) {
set.add(i);
System.out.println(
Thread.currentThread().getName()
+ " added: " + i);
}
};
Thread t1 = new Thread(addNumbers, "Thread-1");
Thread t2 = new Thread(addNumbers, "Thread-2");
t1.start();
t2.start();
}
}
OutputThread-1 added: 1
Thread-1 added: 2
Thread-2 added: 1
Thread-2 added: 2
Explanation:
- Multiple threads can safely add elements to a synchronized set without conflicts.
- Each operation on the set is automatically synchronized.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java