Handling Custom Object With Java Collections
Hi all,
This post is mainly to explain the need of hashcode() and equals() function in java.
Why thus all the Wrapper classes have hashcode() and equals() method implementation and they are known as good key for Hashmap?Let me explain it here with custom object example because in all the wrapper class you will be having hashcode() and equals() method implementation by default
I am going to have Employee object as a key in Hashmap,when you create your own custom object as a key for Hashmap we need override both hashcode() and equals() method .
This is used to maintain unique property in Hashmap key .
Employee class with hashcode and equals method implementation
/**
*
* @author Saradha M
* @since 1.0
*/
public class Employee {
private int empId;
private String name;
/**
* @param empId
* @param name
* @author Saradha M
* @since 1.0
*/
public Employee(int empId, String name) {
super();
this.empId = empId;
this.name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
/* @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + empId;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
*/
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (empId != other.empId)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/**
* @return the empId
* @author Saradha M
* @since 1.0
*/
public int getEmpId() {
return empId;
}
/**
* @param empId the empId to set
* @author Saradha M
* @since 1.0
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @return the name
* @author Saradha M
* @since 1.0
*/
public String getName() {
return name;
}
/**
* @param name the name to set
* @author Saradha M
* @since 1.0
*/
public void setName(String name) {
this.name = name;
}
}
Steps to override hashcode and equals method
while overriding hashcode function we need to take one primary number e.g. 5, 7, 17 or 31
But i would say that better way to override hashcode and equals function is just leave the job to IDE
while overriding hashcode it is always mandatory to override equals function
you can check the above code for equals function
important points to consider while overriding equals function
This post is mainly to explain the need of hashcode() and equals() function in java.
Why thus all the Wrapper classes have hashcode() and equals() method implementation and they are known as good key for Hashmap?Let me explain it here with custom object example because in all the wrapper class you will be having hashcode() and equals() method implementation by default
I am going to have Employee object as a key in Hashmap,when you create your own custom object as a key for Hashmap we need override both hashcode() and equals() method .
This is used to maintain unique property in Hashmap key .
Employee class with hashcode and equals method implementation
/**
*
* @author Saradha M
* @since 1.0
*/
public class Employee {
private int empId;
private String name;
/**
* @param empId
* @param name
* @author Saradha M
* @since 1.0
*/
public Employee(int empId, String name) {
super();
this.empId = empId;
this.name = name;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
/* @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + empId;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
*/
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (empId != other.empId)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/**
* @return the empId
* @author Saradha M
* @since 1.0
*/
public int getEmpId() {
return empId;
}
/**
* @param empId the empId to set
* @author Saradha M
* @since 1.0
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @return the name
* @author Saradha M
* @since 1.0
*/
public String getName() {
return name;
}
/**
* @param name the name to set
* @author Saradha M
* @since 1.0
*/
public void setName(String name) {
this.name = name;
}
}
while overriding hashcode function we need to take one primary number e.g. 5, 7, 17 or 31
But i would say that better way to override hashcode and equals function is just leave the job to IDE
while overriding hashcode it is always mandatory to override equals function
you can check the above code for equals function
important points to consider while overriding equals function
- It is always good to have not null condition
- Add Type check condition
Same like hashcode you can override equals () function also using IDE
Steps to override hashcode() and equals() using Eclipse IDE
- Right click on the Class you have created .
- Source - > Generate hashcode() and equals().
To check for the need of hashcode and equals method implementation , you can just remove the hashcode function add run the program.
compare the output with hashcode function and with out hashcode function ,duplicate objects(empid,empName same)will be added to the collection object when you remove hashcode() function.
Below is my main class
public class AddCustomObjectToCollections {
/**
* @param args
* @author Saradha M
* @since 1.0
*/
public static void main(String[] args) { Employee e1= new Employee(1,"raji"); Employee e2= new Employee(2,"sara"); Employee e3= new Employee(1,"radi"); Employee e4= new Employee(3,"radi"); Employee e5= new Employee(2,"sara"); HashMap<Employee, Integer> empMap = new HashMap<Employee, Integer>(); empMap.put(e1, 1); empMap.put(e2, 2); empMap.put(e3, 3); empMap.put(e4, 1); empMap.put(e5, 4); for(Employee employee:empMap.keySet()){ System.out.println(employee.getEmpId()); } }
}
output with hashcode and equals function
1
2
3
1
output with out hashcode function
2
1
2
3
1
compare the output with hashcode function and with out hashcode function ,duplicate objects(empid,empName same)will be added to the collection object when you remove hashcode() function.
Below is my main class
public class AddCustomObjectToCollections {
/**
* @param args
* @author Saradha M
* @since 1.0
*/
public static void main(String[] args) { Employee e1= new Employee(1,"raji"); Employee e2= new Employee(2,"sara"); Employee e3= new Employee(1,"radi"); Employee e4= new Employee(3,"radi"); Employee e5= new Employee(2,"sara"); HashMap<Employee, Integer> empMap = new HashMap<Employee, Integer>(); empMap.put(e1, 1); empMap.put(e2, 2); empMap.put(e3, 3); empMap.put(e4, 1); empMap.put(e5, 4); for(Employee employee:empMap.keySet()){ System.out.println(employee.getEmpId()); } }
}
output with hashcode and equals function
1
2
3
1
output with out hashcode function
2
1
2
3
1
Comments
Post a Comment