What is HashSet: HashSet is a class that extends AbstractSet and implements set interface which stores unique elements only.
Internal Implementation of HashSet Class: If you check internal implementation of HashSet class it internally uses HashMap to store elements. Below code gives you good understanding of how HashSet internally works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class HashSet<E> extends AbstractSet<E>implements Set<E>, Cloneable, java.io.Serializable { static final long serialVersionUID = -5024744406713321676L; private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); /** * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has * default initial capacity (16) and load factor (0.75). */ public HashSet() { map = new HashMap<>(); } public boolean add(E e) { return map.put(e, PRESENT)==null; } } |
As you can see above whenever you create HashSet Object , HashSet constructor also creates a HashMap object. This HashMap Object will store the elements which you have entered in the HashSet. If you check add method of HashSet in the above code you can see that inside add method put method of HashMap is used to store elements where key is the element you want to store and value is the constant named as PRESENT.
Some important points to be remember:
- HashSet stores unique elements only.
- It gives constant time performance for insertion,retrieval and removal operations.
- It allows only one null element.
- HashSet class is not synchronized ,so can not be accessed by multiple threads .If you want to use HashSet in multi-threaded environment , use Collections.SynchronizedSet() method.
Example o f HashSet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.HashSet; import java.util.Iterator; class Employee { String empName; int empID; String department; public Employee(String empName , int empID, String department) { this.empName = empName; this.empID = empID; this.department = department; } @Override public int hashCode() { return empID; } @Override public boolean equals(Object obj) { Employee emp = (Employee) obj; return (empID == emp.empID); } @Override public String toString() { return "Employee Id is :"+empID+" & employee name is : "+empName+" & department is : "+department; } } public class HashSetDemo { public static void main(String[] args) { HashSet< Employee > set = new HashSet< Employee >(); //Adding elements to HashSet set.add(new Employee("Shobhna", 125128, "Senior Associate")); set.add(new Employee("Gurpal", 74077, "Project Manager")); set.add(new Employee("Shilpi", 15061, "QA")); set.add(new Employee("Nishant", 7974758, "BA")); set.add(new Employee("Anil", 125128, "Software Engineer"));//duplicate element iterator< Employee> it = set.iterator(); while (it.hasNext()) { Employee emp = (Employee) it.next(); System.out.println(emp); } } } |
OutPut:
Employee Id is :15061 & employee name is : Shilpi & department is : QA
Employee Id is :125128 & employee name is : Shobhna & department is : Senior Associate
Employee Id is :74077 & employee name is : Gurpal & department is : Project Manager
Employee Id is :7974758 & employee name is : Nishant & department is : BA
In above example , hashCode() and equals() methods are overridden in Employee class so that Employees will be compared based on empId. Two Employee objects having same empId will be considered as duplicates irrespective of other fields. In given example,duplicate entry for employee Anil will not be stored in the HashSet as empId of Anil is same as of Shobhna.