Online Java Compiler By
JavaTpoint.com
import java.util.*; public class CollectionsSynchronizedSetExample2 { public static void main(String[] args) { Set
set = Collections.synchronizedSet(new HashSet<>()); set.add(101); set.add(102); set.add(103); set.add(104); set.add(105); System.out.println("Set before Synchronized set: " + set); System.out.println("Set after Synchronized set-"); //Using iterator must be synchronized manually synchronized (set) { Iterator
iterator = set.iterator(); while (iterator.hasNext()) { Integer num = iterator.next(); System.out.println(num); } } } }
Output