
write aprogram which shows the use of hashset and iterator (through enter the value through keyboard )

Use of HashSet and Iterator
The given code read the values using scanner class and stored into the HashSet. It does not allow duplicate values. Iterator class iterates the values and display it.
import java.util.*;
public class HashSetExample {
public static void main(String [] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter elements: ");
int size;
HashSet <String>set = new HashSet <String>();
Iterator iterator;
for(int i=1;i<=5;i++){
String st=input.next();
set.add(st);
}
System.out.print("set data: ");
iterator = set.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + " ");
}
System.out.println();
}
}