HashSet Implementation for User Defined Objects.

Earlier we have seen hashset implementation for Integer and String Objects, now we will see implementation of User Defined Objects. First we will create Employee Object which has empName has attribute, and we will try to insert all the employee objects in the hashset.

EMPLOYEE class without overridding equals and hashcode method


package HashSet;

import ArrayList.Employee;

public class HashSetEmployee {

String empName;
 public HashSetEmployee(String empName) {
 // TODO Auto-generated constructor stub
 this.empName = empName;
 }
 public String getEmpName() {
 return empName;
 }
 public void setEmpName(String empName) {
 this.empName = empName;
 }

 public String toString(){
 return empName;
 }

}

Hashset Implementation to add Employee object


package HashSet;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import ArrayList.Employee;

public class HashSetImple {

public HashSetImple() {
 // TODO Auto-generated constructor stub
 }
 public static void main(String[] args) {
 //Custom Class Impl witout override equals and hascode

 Set<HashSetEmployee> employees = new HashSet<>();
 employees.add(new HashSetEmployee("learn"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("learn"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("from"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("examples"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("learn"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("from"));
 System.out.println("-----------------------");
 employees.add(new HashSetEmployee("from"));
 System.out.println("-----------------------");
 System.out.println(employees);

 }
}

Output

-----------------------
-----------------------
-----------------------
-----------------------
-----------------------
-----------------------
-----------------------
[from, from, examples, learn, learn, learn, from]

If we notice the output we will see that hashset is allowing duplicates, but theoretically it should not allow duplicates. Then why is it allowing duplicates?

First we must understand the implementation logic behind hashset.

  • In hashset whenever we add the objects, before adding the object it will check the hashcode value of an object by overridding the hashcode
  • If the calculated hashcode value is already available in the set, then it will call equals method and check the object which we are going to add is equal with the already available object in set.
  • If the object is equal then it will not add it into the set or else it will add the object into the set.

Now we will see the implementation by overriding equals and hashcode method also we will see how it works using a diagram.

Employee Class with overriding equals and hashcode method.


package HashSet;

import ArrayList.Employee;

public class HashSetEmployee {

String empName;
public HashSetEmployee(String empName) {
// TODO Auto-generated constructor stub
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}

public String toString(){
return empName;
}

public boolean equals(Object o){
System.out.println("In equals " +"value is :" +this.empName);
HashSetEmployee employee = (HashSetEmployee)o;
if(employee.getEmpName().equals(this.empName)){
return true;
}
return false;
}

@Override
public int hashCode() {
System.out.println("In hashcode "+"value is :"+this.empName);
int hash = 3;
hash = 7 * hash + this.empName.hashCode();
return hash;
}
}

 Now if we run the “HashSetImple” we will get the following output


In hashcode value is :learn
-----------------------
In hashcode value is :learn
In equals value is :learn
-----------------------
In hashcode value is :from
-----------------------
In hashcode value is :examples
-----------------------
In hashcode value is :learn
In equals value is :learn
-----------------------
In hashcode value is :from
In equals value is :from
-----------------------
In hashcode value is :from
In equals value is :from
-----------------------
[learn, examples, from]

Now we will the diagrammatic  representation of how hashset works by adding two similar values “god“.

Adding the god value for the first time.

Hashset

Adding “God” value for the first time

Adding the god value for the second time and see how hashset avoid duplicates.

equals and hashcode overriding

Adding “God” for the second time, since we have overridden the equals and hashcode it will not add

 

We know if we add Integer and String objects in hashset its avoiding duplicates without overriding the equals and hashcode method.  Because Integer and String class by itself overriden the equals and hashcode method and avoiding the duplicates. But for

6 thoughts on “HashSet Implementation for User Defined Objects.

    • If u have both employee ID and employee name, then employee ID will be unique. So instead of employee name, you have to implement with employee ID.

      Or if you want to consider both then in hashcode methods calculate both employee name and ID and return in. Also in equals method, check for both employee name as well as ID

  1. Hi..
    Can describe ” If u have both employee ID and employee name, then employee ID will be unique. So instead of employee name, you have to implement with employee ID. Because i was trying to do for both but i was working with
    name only.

Leave a comment

Your email address will not be published. Required fields are marked *