Java Collection How to - Convert Set to Synchronized Set








Question

We would like to know how to convert Set to Synchronized Set.

Answer

//w  ww.ja v  a  2 s  .  c o m
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    HashSet hashSet = new HashSet();
    Set set = Collections.synchronizedSet(hashSet);
    System.out.println(set);
  }
}

The code above generates the following result.