Collections synchronizedSet() method in Java with Examples Last Updated : 08 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The synchronizedSet() method of java.util.Collections class is used to return a synchronized (thread-safe) set backed by the specified set. In order to guarantee serial access, it is critical that all access to the backing set is accomplished through the returned set. Syntax: public static <T> Set<T> synchronizedSet(Set<T> s) Parameters: This method takes the set as a parameter to be "wrapped" in a synchronized set. Return Value: This method returns a synchronized view of the specified set. Below are the examples to illustrate the synchronizedSet() method Example 1: Java // Java program to demonstrate // synchronizedSet() method // for String Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Set<String> Set<String> set = new HashSet<String>(); // populate the set set.add("1"); set.add("2"); set.add("3"); // printing the Collection System.out.println("Set : " + set); // create a synchronized set Set<String> synset = Collections.synchronizedSet(set); // printing the set System.out.println("Synchronized set is : " + synset); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Set : [1, 2, 3] Synchronized set is : [1, 2, 3] Example 2: Java // Java program to demonstrate // synchronizedSet() method // for Integer Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Set<Integer> Set<Integer> set = new HashSet<Integer>(); // populate the set set.add(100); set.add(200); set.add(300); // printing the Collection System.out.println("Set : " + set); // create a synchronized set Set<Integer> synset = Collections.synchronizedSet(set); // printing the set System.out.println("Synchronized set is : " + synset); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Set : [100, 200, 300] Synchronized set is : [100, 200, 300] Comment More infoAdvertise with us Next Article Collections synchronizedSet() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Collections unmodifiableSet() method in Java with Examples The unmodifiableSet() method of java.util.Collections class is used to return an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to mod 2 min read Collections newSetFromMap() method in Java with Examples The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to 2 min read SortedSet containsAll() method in Java with Examples The containsAll() method of Java SortedSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C 2 min read Set contains() method in Java with Examples The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme 2 min read Collectors toSet() in Java with Examples Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter 2 min read Like