您好,欢迎访问一九零五行业门户网

Java 中正确使用 hashCode 和 equals 方法

在这篇文章中,我将告诉大家我对hashcode和equals方法的理解。我将讨论他们的默认实现,以及如何正确的重写他们。我也将使用apache commons提供的工具包做一个实现。
目录:
hashcode()和equals()的用法
重写默认实现
使用apache commons lang包重写hashcode()和equals()
需要注意记住的事情
当使用orm的时候特别要注意的
hashcode()和equals()定义在object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法。
使用hashcode()和equals()
hashcode()方法被用来获取给定对象的唯一整数。这个整数被用来确定对象被存储在hashtable类似的结构中的位置。默认的,object类的hashcode()方法返回这个对象存储的内存地址的编号。
重写默认的实现
如果你不重写这两个方法,将几乎不遇到任何问题,但是有的时候程序要求我们必须改变一些对象的默认实现。
来看看这个例子,让我们创建一个简单的类employee
public class employee { private integer id; private string firstname; private string lastname; private string department; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getdepartment() { return department; } public void setdepartment(string department) { this.department = department; } }
上面的employee类只是有一些非常基础的属性和getter、setter.现在来考虑一个你需要比较两个employee的情形。
public class equalstest { public static void main(string[] args) { employee e1 = new employee(); employee e2 = new employee(); e1.setid(100); e2.setid(100); //prints false in console system.out.println(e1.equals(e2)); } }
毫无疑问,上面的程序将输出false,但是,事实上上面两个对象代表的是通过一个employee。真正的商业逻辑希望我们返回true。
为了达到这个目的,我们需要重写equals方法。
public boolean equals(object o) { if(o == null) { return false; } if (o == this) { return true; } if (getclass() != o.getclass()) { return false; } employee e = (employee) o; return (this.getid() == e.getid()); }
在上面的类中添加这个方法,eauqlstest将会输出true。
so are we done?没有,让我们换一种测试方法来看看。
import java.util.hashset; import java.util.set; public class equalstest { public static void main(string[] args) { employee e1 = new employee(); employee e2 = new employee(); e1.setid(100); e2.setid(100); //prints 'true' system.out.println(e1.equals(e2)); set<employee> employees = new hashset<employee>(); employees.add(e1); employees.add(e2); //prints two objects system.out.println(employees); }
上面的程序输出的结果是两个。如果两个employee对象equals返回true,set中应该只存储一个对象才对,问题在哪里呢?
我们忘掉了第二个重要的方法hashcode()。就像jdk的javadoc中所说的一样,如果重写equals()方法必须要重写hashcode()方法。我们加上下面这个方法,程序将执行正确。
@override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + getid(); return result; }
使用apache commons lang包重写hashcode() 和equals()方法
apache commons 包提供了两个非常优秀的类来生成hashcode()和equals()方法。看下面的程序。
import org.apache.commons.lang3.builder.equalsbuilder; import org.apache.commons.lang3.builder.hashcodebuilder; public class employee { private integer id; private string firstname; private string lastname; private string department; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getdepartment() { return department; } public void setdepartment(string department) { this.department = department; } @override public int hashcode() { final int prime = 31; return new hashcodebuilder(getid()%2==0?getid()+1:getid(), prime). tohashcode(); } @override public boolean equals(object o) { if (o == null) return false; if (o == this) return true; if (o.getclass() != getclass()) return false; employee e = (employee) o; return new equalsbuilder(). append(getid(), e.getid()). isequals(); } }
如果你使用eclipse或者其他的ide,ide也可能会提供生成良好的hashcode()方法和equals()方法。
需要注意记住的事情
尽量保证使用对象的同一个属性来生成hashcode()和equals()两个方法。在我们的案例中,我们使用员工id。
eqauls方法必须保证一致(如果对象没有被修改,equals应该返回相同的值)
任何时候只要a.equals(b),那么a.hashcode()必须和b.hashcode()相等。
两者必须同时重写。
当使用orm的时候特别要注意的
如果你使用orm处理一些对象的话,你要确保在hashcode()和equals()对象中使用getter和setter而不是直接引用成员变量。因为在orm中有的时候成员变量会被延时加载,这些变量只有当getter方法被调用的时候才真正可用。
例如在我们的例子中,如果我们使用e1.id == e2.id则可能会出现这个问题,但是我们使用e1.getid() == e2.getid()就不会出现这个问题。
更多java 中正确使用 hashcode 和 equals 方法。
其它类似信息

推荐信息