2012-04-15 147 views
0

我有一个POJO,在我所定义的hashCode方法我认为是的..正确实现hashCode()方法

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

} 

但因为我使用的Eclipse IDE,它也提供了我的自动生成散列码,其是..

 @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; 
} 

现在我的疑问是,这两个之间的差异,哪一个实现更好.. ..!我完全POJO是...

enter codepackage CollectionsPrac; 

公共类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; 
    } 

} 这里

回答

4

基于Eclipse的产生hashCode()敏感得多,当谈到在你的POJO的微小变化。例如。如果切换jobname值彼此,你hashCode()将返回相同的值(加法是可交换的),而花哨的Eclipse版本将返回完全不同的东西:

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 

又见

+0

嗨,所以请你告诉我我的hashcode()实现有什么问题。!1 – Neera 2012-04-15 18:06:35

+1

你的版本比Eclipse的版本产生更多(错误)散列冲突。 – 2012-04-15 18:13:17

+0

@ user1334074:查看我的更新。基本上希望'hashCode()'应该为稍微不同的对象返回不同的结果。 – 2012-04-15 18:16:15

2

最重要的区别是,如果您的实现将抛出NullPointerException如果jobnamenull

此外,方法eclipse生成更不规则哈希码的结果,这在理论上意味着哈希表退化和性能较差的可能性较低,但实际上可能无关紧要,因为java.util.HashMap使用辅助哈希函数在使用之前扰乱哈希码。

+0

嗨迈克尔,你能否提供这个类的哈希码()的正确实现,这将是一个很大的帮助..!提前致谢..! – Neera 2012-04-15 18:10:44

+0

@ user1334074:你为什么不使用eclipse生成的那个? – 2012-04-15 18:13:40

+0

嗨Miachel,我正在使用这一个仪式现在,如果你可以请给我提供的hashcode()只是我已经生成,这将是一个很好的帮助..! – Neera 2012-04-15 18:16:06