目录:
使用hashCode()和equals()
hashCode()方法被用来获取给定对象的唯一整数。这个整数被用来确定对象被存储在HashTable类似的结构中的位置。默认的,Object类的hashCode()方法返回这个对象存储的内存地址的编号。
重写默认的实现
如果你不重写这两个方法,将几乎不遇到任何问题,但是有的时候程序要求我们必须改变一些对象的默认实现。
来看看这个例子,让我们创建一个简单的类Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 | 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)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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); } |
1 2 3 4 5 6 7 8 | @Override public int hashCode() { final int PRIME = 31 ; int result = 1 ; result = PRIME * result + getId(); return result; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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(); } } |
需要注意记住的事情
Java 中正确使用 hashCode 和 equals 方法
原文:http://www.blogjava.net/paulwong/archive/2015/12/01/428439.html