博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java:重写equals()和hashCode()
阅读量:4053 次
发布时间:2019-05-25

本文共 3141 字,大约阅读时间需要 10 分钟。

以下内容总结自《Effective Java》。
1.何时需要重写equals()
当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念)。
2.设计equals()
[1]使用instanceof操作符检查“实参是否为正确的类型”。
[2]对于类中的每一个“关键域”,检查实参中的域与当前对象中对应的域值。
[2.1]对于非float和double类型的原语类型域,使用==比较;
[2.2]对于对象引用域,递归调用equals方法;
[2.3]对于float域,使用
Float.floatToIntBits(
afloat
)转换为int,再使用==比较;
[2.4]对于double域,使用
Double.doubleToLongBits(
adouble
) 转换为int,再使用==比较;
[2.5]对于数组域,调用Arrays.equals方法。
3.当改写equals()的时候,总是要改写hashCode()
根据一个类的equals方法(改写后),两个截然不同的实例有可能在逻辑上是相等的,但是,根据Object.hashCode方法,它们仅仅是两个对象。因此,违反了“相等的对象必须具有相等的散列码”。
4.设计hashCode()
[1]把某个非零常数值,例如17,保存在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()。
<td 230,="" 230)="" none="" repeat="" scroll="" 0%="" 50%;="" width:="" 426.1pt;="" -moz-background-clip:="" -moz-initial;="" -moz-background-origin:="" -moz-background-inline-policy:="" -moz-initial;"="" valign="top" width="568" style="padding: 0cm 5.4pt; margin: 0px; background-color: white; border: 1pt solid windowtext; ">
package
 com.zj.unit;
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
;
 
    
public
 
boolean
 equals(Object o) {
       
if
 (!(o 
instanceof
 Unit))
           
return
 
false
;
       Unit unit = (Unit) o;
       
return
 unit.
ashort
 == 
ashort
              && unit.
achar
 == 
achar
              && unit.
abyte
 == 
abyte
              && unit.
abool
 == 
abool
              && unit.
along
 == 
along
              && Float.
floatToIntBits(unit.
afloat
) == Float
                     
.floatToIntBits(
afloat
)
              && Double.
doubleToLongBits(unit.
adouble
) == Double
                     .
doubleToLongBits(
adouble
)
              
&& unit.
aObject
.equals(
aObject
)
&& equalsInts(unit.
ints
)
              && equalsUnits(unit.
units
);
    }
 
    
private
 
boolean
 equalsInts(
int
[] aints) {
       
return
 Arrays.equals(
ints
, aints);
    }
 
    
private
 
boolean
 equalsUnits(Unit[] aUnits) {
       
return
 Arrays.equals(
units
, aUnits);
    }
 
    
public
 
int
 hashCode() {
       
int
 result = 17;
       result = 37 * result + (
int
ashort
;
       result = 37 * result + (
int
achar
;
       result = 37 * result + (
int
abyte
;
       result = 37 * result + (
abool
 ? 0 : 1);
       result = 37 * result + (
int
) (
along
 ^ (
along
 >>> 32));
       result = 37 * result + Float.
floatToIntBits(
afloat
);
       
long
 tolong = Double.doubleToLongBits(
adouble
);
       result = 37 * result + (
int
) (tolong ^ (tolong >>> 32));
       result = 37 * result + 
aObject
.hashCode();
       result = 37 * result + intsHashCode(
ints
);
       result = 37 * result + unitsHashCode(
units
);
       
return
 result;
    }
 
    
private
 
int
 intsHashCode(
int
[] aints) {
       
int
 result = 17;
       
for
 (
int
 i = 0; i < aints.
length
; i++)
           result = 37 * result + aints[i];
       
return
 result;
    }
 
    
private
 
int
 unitsHashCode(Unit[] aUnits) {
       
int
 result = 17;
       
for
 (
int
 i = 0; i < aUnits.
length
; i++)
           result = 37 * result + aUnits[i].hashCode();
       
return
 result;
    }
}
 

转载地址:http://ontci.baihongyu.com/

你可能感兴趣的文章
yuv420 还原为RGB图像
查看>>
LED恒流驱动芯片
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt 创建异形窗体
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
GNU hello代码分析
查看>>
Qt继电器控制板代码
查看>>
wpa_supplicant控制脚本
查看>>
gstreamer相关工具集合
查看>>
RS232 四入四出模块控制代码
查看>>
linux 驱动开发 头文件
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>