Wednesday, July 3, 2024
HomeLanguagesJavaCollections synchronizedSet() method in Java with Examples

Collections synchronizedSet() method in Java with Examples

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 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 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]

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments