Example:
package com.w3schools;
import java.util.HashSet;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
//Create HashSet object
HashSet<String> hashSet = new HashSet<String>();
//Add elements to HashSet
hashSet.add("Jai");
hashSet.add("Mahesh");
hashSet.add("Vivek");
hashSet.add("Naren");
hashSet.add("Vishal");
hashSet.add("Hemant");
Iterator<String> iterator = hashSet.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
} |
package com.w3schools;
import java.util.HashSet;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
//Create HashSet object
HashSet<String> hashSet = new HashSet<String>();
//Add elements to HashSet
hashSet.add("Jai");
hashSet.add("Mahesh");
hashSet.add("Vivek");
hashSet.add("Naren");
hashSet.add("Vishal");
hashSet.add("Hemant");
Iterator<String> iterator = hashSet.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Output
Naren
Jai
Vivek
Hemant
Mahesh
Vishal |
Naren
Jai
Vivek
Hemant
Mahesh
Vishal