0

I have a pojo , in which I have defined the hashcode method myself that is..

 public int hashCode()
     {       
     return name.hashCode()+job.hashCode()+salary;       

 }

But since I am using eclipse IDE , it also provide my automatically generated hashcode which is..

     @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((job == null) ? 0 : job.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + salary;
    return result;
}

Now my query is that what's the differnce between the two, which one implemenatation is better..! My complete pojo is...

enter codepackage CollectionsPrac;

public class Employee {

 String name,job;
 int salary;


 public Employee(String n , String j, int t )
 {
     this.name= n;
     this.job=j;
     this.salary= t;         
 }


 @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((job == null) ? 0 : job.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + salary;
    return result;
}


 /*@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 (job == null) {
        if (other.job != null)
            return false;
    } else if (!job.equals(other.job))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (salary != other.salary)
        return false;
    return true;
}
 */

/* @Override
 public int hashCode()
     {       
     return name.hashCode()+job.hashCode()+salary;       

 }*/

 @Override
    public boolean equals(Object obj) {  
     if (this == obj)  
    {  
        return true;   
    }  
    // make sure o can be cast to this class  
    if (obj == null || obj.getClass() != getClass())  
    {  
        // cannot cast  
        return false;  
    }           

     Employee e = (Employee) obj;   
     return this.name.equals(e.name)&&this.job.equals(e.job)&&this.salary==e.salary;
 }

 @Override
 public String toString() {
        return name+"\t" +"\t"+  job +"\t"+ salary;
    }

} here

Neera
  • 21
  • 1
  • 8

2 Answers2

4

The Eclipse-generate hashCode() is much more sensitive when it comes to small changes in your POJO. E.g. if you switch job and name values with each other, your hashCode() will return the same value (addition is commutative) while the fancy Eclipse version will return something completely different:

System.out.println(new Employee("John", "Blacksmith", 100).hashCode());
System.out.println(new Employee("Blacksmith", "John", 100).hashCode());

//your version of hashCode() produces identical result:
376076563
376076563

//Eclipse version:
-1520263300
926019626

See also

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Hi , so please can you tell me whats wrong with my implemnentation of hashcode()..!1 – Neera Apr 15 '12 at 18:06
  • 1
    Your version generates many more (bad) hash collisions than Eclipse's version. – Louis Wasserman Apr 15 '12 at 18:13
  • @user1334074: see my update. Basically it is desired that `hashCode()` should return different results for slightly different objects. – Tomasz Nurkiewicz Apr 15 '12 at 18:16
  • Hi Tomasz ,Thanks a lot , so practically which hashcode() implementaion I should go, eclipse one..!! – Neera Apr 15 '12 at 18:18
  • Hi Louis , thanks a lot...for explaining that my hashcode generates generates many more (bad) hash collisions than Eclipse's version,,could you please explain this alo in simpler terms , that will make understandings more clear..!thanks in advance – Neera Apr 15 '12 at 18:25
2

The most important difference is that your implementation will throw a NullPointerException if job or name is null.

Additionally, the method eclipse generates results in a more irregular hashcode, which in theory means there is a lower likelihood of hashtables degenerating and having poor performance, but in practice that probably won't matter since java.util.HashMap uses an auxiallary hash function to scramble the hashcode before using it.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • Hi Michael, could you please provide the correct implementation of hash code() for this class that will be a great help..!! Thanks in advance..! – Neera Apr 15 '12 at 18:10
  • @user1334074: Why don't you just use the one generated by eclipse? – Michael Borgwardt Apr 15 '12 at 18:13
  • Hi Miachel ,I am using that one rite Now, if you could please provide me with the hashcode() just I hve generated that will be a great help..!! – Neera Apr 15 '12 at 18:16