Java中equals()与hashCode()的原理与设计

时间:2022-08-19 16:47:30
1.何时需要重写equals()
当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念)。
2.为什么改写equals()的时候,总是要改写hashCode()
两个原则:hashCode()的返回值和equals()的关系如下:
  • 如果x.equals(y)返回“true”,那么x和y的hashCode()必须相等。
  • 如果x.equals(y)返回“false”,那么x和y的hashCode()有可能相等,也有可能不等。
简单的说,“相等的对象必须具有相等的散列码”。

3.什么是equals()与如何设计equals()
它是对象内容的比较。涵盖内容比对象参阅值比较更丰富。什么是对象参阅值比较:就是两个对象的参阅变量的值的比较。值是一个数字,通过数字来鉴别不同对象的代号,这种默认的比较方式,在object对象中已有设计实现。 而对象内容的比较才是设计equals()的真正目的。
参考《effective java》上equals的几个特性
  • 对称性:如果x.equals(y)返回是“true”,那么y.equals(x)也应该返回是“true”。
  • 自反性:x.equals(x)必须返回是“true”。
  • 类推性:如果x.equals(y)返回是“true”,而且y.equals(z)返回是“true”,那么z.equals(x)也应该返回是“true”。
  • 一致性:如果x.equals(y)返回是“true”,只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是“true”。
  • 任何情况下,x.equals(null),永远返回是“false”;x.equals(和x不同类型的对象)永远返回是“false”。

设计步骤: [1]使用getClass()方法检查“实参是否为正确的类型”。 [2]对于类中的每一个“关键域”,检查实参中的域与当前对象中对应的域值。 [2.1]对于非floatdouble类型的原语类型域,使用==比较; [2.2]对于对象引用域,递归调用equals方法; [2.3]对于float域,使用Float.floatToIntBits(afloat)转换为int,再使用==比较; [2.4]对于double域,使用Double.doubleToLongBits(adouble) 转换为long,再使用==比较; [2.5]对于数组域,调用Arrays.equals方法。
4.什么是hashcode()与如何设计hashCode()
这 个函数返回的就是一个用来进行哈希操作的整型代号,请不要把这个代号和前面所说的参阅变量所代表的代号弄混了。后者不仅仅是个代号还具有在内存中才查找对象的位置的功能。hashCode()所返回的值是用来分类对象在一些特定的收集对象中的位置。
设计步骤: [1]把某个非零常数值,例如1,保存在int变量result中; [2]对于对象中每一个关键域f(指equals方法中考虑的每一个域): [2.1]boolean型,计算(f ? 0 : 1); [2.2]byte,char,short型,计算(int); [2.3]long型,计算(int) (f ^ (f>>>32)); [2.4]float型,计算Float.floatToIntBits(afloat); [2.5]double型,计算Double.doubleToLongBits(adouble)得到一个long,再执行[2.3]; [2.6]对象引用,递归调用它的hashCode方法; [2.7]数组域,对其中每个元素调用它的hashCode方法。 [3]将上面计算得到的散列码保存到int变量c,然后执行 result=37*result+c; [4]返回result
5.符合上面的原则的equals与hashcode的示例代码
import java.util.Arrays;

public class Unit {
private short ashort;
private char achar;
private byte abyte;
private boolean abool;
private long along;
private float afloat;
private double adouble;
private Unit aObject;
private int[] ints;
private Unit[] units;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((aObject == null) ? 0 : aObject.hashCode());
result = prime * result + (abool ? 1231 : 1237);
result = prime * result + abyte;
result = prime * result + achar;
long temp;
temp = Double.doubleToLongBits(adouble);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + Float.floatToIntBits(afloat);
result = prime * result + (int) (along ^ (along >>> 32));
result = prime * result + ashort;
result = prime * result + Arrays.hashCode(ints);
result = prime * result + Arrays.hashCode(units);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Unit other = (Unit) obj;
if (aObject == null) {
if (other.aObject != null)
return false;
} else if (!aObject.equals(other.aObject))
return false;
if (abool != other.abool)
return false;
if (abyte != other.abyte)
return false;
if (achar != other.achar)
return false;
if (Double.doubleToLongBits(adouble) != Double
.doubleToLongBits(other.adouble))
return false;
if (Float.floatToIntBits(afloat) != Float.floatToIntBits(other.afloat))
return false;
if (along != other.along)
return false;
if (ashort != other.ashort)
return false;
if (!Arrays.equals(ints, other.ints))
return false;
if (!Arrays.equals(units, other.units))
return false;
return true;
}
}