Online Java Compiler By
JavaTpoint.com
import java.util.*; public class HashSetIteratorExample3 { public static void main(String[] args) throws InterruptedException { //Get the HashMap object HashSet studentSet = init(); //Display the contents of set Iterator
it = studentSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } private static HashSet init() { //Create HashSet object HashSet
studentSet = new HashSet<>(); //Add contents to the set studentSet.add("Rahul"); studentSet.add("Mohan"); studentSet.add("Karan"); return studentSet; } }
Output