Java Collection Tutorial - Java Collections .synchronizedSet ( Set <T> s)








Syntax

Collections.synchronizedSet(Set <T> s) has the following syntax.

public static <T> Set <T> synchronizedSet(Set <T> s)

Example

In the following code shows how to use Collections.synchronizedSet(Set <T> s) method.

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/*ww  w  . j a v  a  2 s. c  om*/
public class Main {
   public static void main(String[] args) {
      // create set
      Set<String>  set = new HashSet<String> ();
      
      // populate the set
      set.add("A");
      set.add("B");
      set.add("from");
      set.add("java2s.com");
      
      
      // create a synchronized set
      Set<String>  synset = Collections.synchronizedSet(set);
     
      System.out.println("Synchronized set is :"+synset);
   }
}

The code above generates the following result.