1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* Get Synchronized Set from Java HashSet example This java example shows how to get a synchronized Set from Java HashSet using synchronizedSet method of Collections class. */ import java.util.HashSet; import java.util.Collections; import java.util.Set; public class GetSynchronizedSetFromHashSetExample { public static void main(String[] args) { //create HashSet object HashSet hashSet = new HashSet(); /* Java HashSet is NOT synchronized. To get synchronized Set from HashSet use static void synchronizedSet(Set set) method of Collections class. */ Set set = Collections.synchronizedSet(hashSet); /* Use this set object to prevent any unsynchronized access to original HashSet object. */ } } |
Add Comment