您的位置:首页 > 其它

改写equals与hashCode的简单方法

2012-11-16 18:23 295 查看
Apache Commons Lang库中的EqualsBuilder与HashCodeBuilder类可以简化Java类中equals与hashCode方法的改写过程。

关于如何改变 Java类中的equals与hashCode方法,Effective Java一书中提供了一个行之有效的方法。改写equals方法的步骤为:

使用==操作符检查“实参是否为指向对象的一个引用”。
使用instanceof操作符检查“实参是否为正确的类型”。
把实参转换到正确的类型。
对于该类中每一个“关键”域,检查实参中的域与当前对象中对应的域值是否匹配。

其中最后一步书中的描述为:

对于既不是float也不是double类型的简单类型域,可以使用==操作符进行比较;
对 于对象引用域,可以递归地调用equals方法;
对于float域,先使用Float.floatToIntBits转换成int类型 的值,然后使用==操作符比较int类型的值;
对于double域,先使用Double.doubleToLongBits转换成 long类型的值,然后使用==操作符比较long类型的值;
对于数组域,把以上这些指导原则应用到每个元素上。

有些对象引用域包含null是合法的,所以为了避免可能导致NullPointerException 异常,使用下面的习惯用法来比较这样的域:

(field == o.field || (field != null && field.equals(o.field))

改写equals时总是要改写hashCode,改写hashCode方法的步骤为:

把某个非零常数值比如说17, 保存在一个叫result的int类型的变量中。
对于对象中每一个关键域f(指equals方法中考虑的每一个域),完成以下步骤:

为该域计算int类型的散列码c:

如果该域是boolean类型,则计算(f ? 0 : 1)。
如果该域是byte、 char、short或者int类型,则计算(int)f。
如果该域是long类型,则计算(int)(f ^ (f >>> 32))。
如果该域是float类型,则计算Float.floatToIntBits(f)。
如 果该域是double类型,则计算Double.doubleToLongBits(f)得到一个long类型的值,然后按照步骤2.1.3,对该 long型值计算散列值。

如果该域是一个对象引用,并且该类的equals方法通过递归调用equals的方式来比较这个域,则同样对 这个域递归调用hashCode。如果这个域的值为null,则返回0。

如果该域是一个数组,则把每一个元素当做单独的域来处理。

按照下面的公式,把步骤a中计算得到的散列码c组合到result中:result = 37 * result + c

返回result。

上述方案中关于“关键”域的计算过程是比较复杂的,看一看下面这个简单类Point实现的equals与 hashCode方法:

Default

public class Point {
private final int x;
private final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public boolean equals(Object other) {
// step 1
if (other == this) {
return true;
}

// step 2
if (!(other instanceof Point)) {
return false;
}

// step 3
Point that = (Point) other;

// step 4
boolean result =
(this.getX() == that.getX()) &&
(this.getY() == that.getY());
return result;
}

@Override
public int hashCode() {
// step 1
int result = 17;

// step 2
result = 37 * result + getX();
result = 37 * result + getY();

// step 3
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

public
class Point
{
private
final int
x;
private
final int
y;

public
Point(int
x, int
y) {
this.x
= x;
this.y
= y;
}

public
int getX()
{
return
x;
}

public
int getY()
{
return
y;
}

@Override
public
boolean equals(Object
other)
{
// step 1
if
(other
== this)
{
return
true;
}

// step 2
if
(!(other
instanceof Point))
{
return
false;
}

// step 3
Point
that =
(Point)
other;

// step 4
boolean
result =
(this.getX()
== that.getX())
&&
(this.getY()
== that.getY());
return
result;
}

@Override
public
int hashCode()
{
// step 1
int
result =
17;

// step 2
result
= 37
* result
+ getX();
result
= 37
* result
+ getY();

// step 3
return
result;
}
}

如果“关键”域数量较多,实现上难免出现错误。Commons Lang库为我们提供了两个工具类用于简化equals方法的第4步及hashCode方法的第2步,它们分别为EqualsBuilder与 HashCodeBuilder类。如上例的Commons Lang库版本:

Default

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Point {
private final int x;
private final int y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public boolean equals(Object other) {
// step 1
if (other == this) {
return true;
}

// step 2
if (!(other instanceof Point)) {
return false;
}

// step 3
Point that = (Point) other;

// step 4
return new EqualsBuilder()
.append(this.getX(), that.getX())
.append(this.getY(), that.getY())
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(this.getX())
.append(this.getY())
.toHashCode();
}
}

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

import
org.apache.commons.lang.builder.EqualsBuilder;
import
org.apache.commons.lang.builder.HashCodeBuilder;

public
class Point
{
private
final int
x;
private
final int
y;

public
Point(int
x, int
y) {
this.x
= x;
this.y
= y;
}

public
int getX()
{
return
x;
}

public
int getY()
{
return
y;
}

@Override
public
boolean equals(Object
other)
{
// step 1
if
(other
== this)
{
return
true;
}

// step 2
if
(!(other
instanceof Point))
{
return
false;
}

// step 3
Point
that =
(Point)
other;

// step 4
return
new EqualsBuilder()
.append(this.getX(),
that.getX())
.append(this.getY(),
that.getY())
.isEquals();
}

@Override
public
int hashCode()
{
return
new HashCodeBuilder(17,
37)
.append(this.getX())
.append(this.getY())
.toHashCode();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: