Hashset Example in Java Collection Framework. This Java Hashset example describes the basic operations performed on the Hashset in Collection Framework.
Program:
import java.util.*;
class HashSetDemo{
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add(“Hello”);
hs.add(“How are you”);
hs.add(“fine”);
System.out.println(“HashSet: “+hs);
hs.remove(“fine”);
System.out.print(“\n HashSet after removing : “+ hs);
hs.add(“Not Bad”);
System.out.print(“\n HashSet after adding : “+ hs);
Iterator it = hs.iterator();
System.out.print(“\n HashSet-element: \n”);
while(it.hasNext())
System.out.println(” “+it.next());
}
}
Result:
HashSet: [fine, How are you, Hello]
HashSet after removing : [How are you, Hello]
HashSet after adding : [How are you, Hello, Not Bad]
HashSet-element:
How are you
Hello
Not Bad