Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实现hashCode和equals方法_Java_Equals_Hashcode - Fatal编程技术网

Java 如何实现hashCode和equals方法

Java 如何实现hashCode和equals方法,java,equals,hashcode,Java,Equals,Hashcode,我应该如何在Java中为以下类实现hashCode()和equals() class Emp { int empid ; // unique across all the departments String name; String dept_name ; String code ; // unique for the department } 如果代码是唯一的(即您的业务密钥),那么最好只将代码用于equals和hashCode-将业务密钥(代码)与对象id(id)分

我应该如何在Java中为以下类实现
hashCode()
equals()

class Emp 
{
  int empid ; // unique across all the departments 
  String name;
  String dept_name ;
  String code ; // unique for the department 
}
如果代码是唯一的(即您的业务密钥),那么最好只将代码用于equals和hashCode-将业务密钥(代码)与对象id(id)分开是一种很好的做法


这是一个很好的阅读:(不仅对Hibernate本身有效)

在Eclipse中,右键单击->源->生成hashCode()和equals()给出以下内容:

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (code == null ? 0 : code.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 (!(obj instanceof Emp))
        return false;
    Emp other = (Emp) obj;
    return code == null ? other.code == null : code.equals(other.code);
}

我选择了“代码”作为唯一字段

在equals中用于确定两个对象是否相同的值是创建哈希代码所需的值

public boolean equals(Object o) {

    boolean result = false;

    if(o instanceof CategoryEnum) {

        CategoryEnum ce = (CategoryEnum) o;
        result = ce.toString().equals(name);

    }       
    return result;

}


public int hashCode()
{
  int hash = 6;
  hash += 32 * name.hashCode();
  return hash;
}   
equals()和hashcode(),它们有很多不同的位置。 equals(),如果我们不从对象重写它,它表示两个变量是否指向同一个对象堆

public  Class Student(){
  private int id;
  private  name;
  public Student(int id,String name){
  this.name=name;
  this.id=id; 
}

public void main(String[] args){
  Student A=new Student(20,'Lily');
  Student B=new Student(20,'Lily');
  boolean flag=A.equals(B)//flag=flase;
/*
 *Although they attribute the same, but they are two different objects, they point to     different memory
 */


@Override
public boolean equals(Object obj) {


  if (obj == null) {
    return false;
  }
  if (this == obj) {
    return true;
  }

  if (this.getClass() != obj.getClass()) {
    return false;
  }
  Student s=(Student)obj;
  return new Integer(this.id).equals(new Integer(s.id))&&this.name.equals(s.name);
  }

/**
  *Sometimes even though we Override  the equals, but we still can not determine whether   the *two objects the same,
  *In the collection object, such as HashSet, this time we have to Override the hashoCode ()
  */

public int hashCode(){
  return id + name.hashCode() ;
}
尝试此代码,使用


番石榴有助于创建它们的方法。您告诉它要考虑哪些字段,它将为您处理空值,并为哈希代码计算素数

IDE还可以根据您选择的字段生成它们

将其委托给这样的工具的好处是,您可以得到一个标准的解决方案,并且不必担心遍布整个项目的各种实现的bug和维护


下面是一个使用Guava并由IntelliJ插件生成的示例:

它不是Java源代码。或者您是否创建了自己的名为
string
的类?你们真的应该看看这个:我看到Joachim在你们的代码样本中改变了几个错误。尽管如此,我还是建议查看我在上一篇评论中发布的链接。我使用的是java的String ClassAll see:,这解释了如何编写am Equality方法。它对于一个部门来说似乎是唯一的,因此这表明部门可以共享相同的“代码”。无论如何,如果你问我一个相当模糊的问题。嗯。。。我通常会比较equals()方法中的所有字段,除非您确实有充分的理由不这样做……那么,如果来自不同部门的两个
Emp
具有相同的代码,它们是否相等?您至少需要在这个实现中添加
部门名称
。我接受这个答案,因为我知道需要做什么。jutky假设代码是唯一标识符;是多余的,因为instanceof检查是否为空使用instanceof检查会导致声纳严重问题。所以最好使用if(getClass()!=other.getClass()){return false;}(我的答案是no-1),这不是完全正确的。对于hashCode,只使用子集是完全合法的(例如,对于equals,使用code和name,对于hashCode,只使用code)。使用常量hashCode(
public int hashCode(){return 42;}
)甚至是合法的-它破坏了哈希集合(HashMap、HashSet等)的性能,但它们仍能正常工作。因此,它比无效的hashCode方法要好。唯一的规则是:如果两个对象相等(
a.equals(b)
),它们必须具有相同的哈希代码(
a.hashCode()==b.hashCode()
)。如果它们不相等,哈希代码可能仍然相等。
public int hashCode() {
    return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
        append(empid).
        append(name).
        append(dept_name ).
        append(code ).
        toHashCode();
}

public boolean equals(Object obj) {

    if (obj == this)
        return true;
    if (!(obj instanceof Person))
        return false;

    Emp rhs = (Emp) obj;
    return new EqualsBuilder().
        // if deriving: appendSuper(super.equals(obj)).
        append(name, rhs.name).
        isEquals();
}