HashSet Class
- The HashSet works on the mechanism of hashing. HashSet extends the AbstractSet class and implements Set interface.
- The HashSet store the data in the form of hash table i.e. key value format.
Example : Program to implement the methods of HashSet Class
import java.util.*;
public class HashSetDemo
{
public static void main(String args[])
{
//creates hashset
HashSet<String> hs = new HashSet<String>();
System.out.println("Initional size of LinkedList: "+hs.size());
System.out.println("HashSet is empty: "+hs.isEmpty());
//add element in hashset
hs.add("Red");
hs.add("Blue");
hs.add("Pink");
hs.add("Green");
hs.add("Pink");
System.out.println("HashSet elements after adding: "+hs);
//delete the element
hs.remove("Green");
System.out.println("HashSet elements after delete: "+hs);
System.out.println("HashSet is empty: "+hs.isEmpty());
}
}
Output:
Initional size of LinkedList: 0
HashSet is empty: true
HashSet elements after adding: [Red, Pink, Blue, Green]
HashSet elements after delete: [Red, Pink, Blue]
HashSet is empty: false


