如何重写hashcode方法
1个回答
展开全部
我们应该先了解java判断两个对象是否相等的规则:
首先,判断两个对象的hashCode是否相等;
如果不相等,认为两个对象也不相等;
如果相等,则判断两个对象用equals运算是否相等;
如果不相等,认为两个对象也不相等;
如果相等,认为两个对象相等。
当我们重写了一个类的equals()方法后,我们一定要记住同时也要重写hashCode()方法。
下面介绍如何来重写hashCode()方法。通常重写hashCode()方法按以下设计原则实现。
(1)把某个非零素数,例如17,保存在int型变量result中。
(2)对于对象中每一个关键域f(指equals方法中考虑的每一个域)参照以下原则处理。
boolean型,计算(f?0:1)。
byte、char和short型,计算(int)f。
long型,计算(int)(f^(f>>32))。
float型,计算Float.floatToIntBits(f)。
double型,计算Double.doubleToLongBits(f)得到一个long,再执行long型的处理。
对象引用,递归调用它的hashCode()方法。
数组域,对其中的每个元素调用它的hashCode()方法。
(3)将上面计算得到的散列码保存到int型变量c,然后执行result = 37 * result + c。
(4)返回result。
重写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
String
astr;
private
int[]
ints;
private
String[]
strs;
@Override
public
boolean
equals(Object
obj)
{
if
(obj
==
null)
{
return
false;
}
if
(!(obj
instanceof
Unit))
{
return
false;
}
Unit
o
=
(Unit)
obj;
return
o.ashort
==
ashort
&&
o.achar
==
achar
&&
o.abyte
==
abyte
&&
o.abool
==
abool
&&
o.along
==
along
&&
Float.floatToIntBits(o.afloat)
==
Float
.floatToIntBits(afloat)
&&
Double.doubleToRawLongBits(o.adouble)
==
Double
.doubleToRawLongBits(adouble)
&&
o.astr.equals(astr)
&&
Arrays.equals(ints,
o.ints)
&&
Arrays.equals(strs,
o.strs);
}
@Override
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.doubleToRawLongBits(adouble);
result
=
37
*
result
+
(int)
(tolong
^
(tolong
>>>
32));
result
=
37
*
result
+
astr.hashCode();
result
=
37
*
result
+
intsHashCode(ints);
result
=
37
*
result
+
unitsHashCode(strs);
return
result;
}
private
int
intsHashCode(int[]
aints)
{
int
result
=
17;
for
(int
i
:
aints)
{
result
=
37
*
result
+
i;
}
return
result;
}
private
int
unitsHashCode(String[]
astrs)
{
int
result
=
17;
for
(String
s
:
astrs)
{
result
=
37
*
result
+
s.hashCode();
}
return
result;
}
public
void
setAshort(short
ashort)
{
this.ashort
=
ashort;
}
public
void
setAchar(char
achar)
{
this.achar
=
achar;
}
public
void
setAbyte(byte
abyte)
{
this.abyte
=
abyte;
}
public
void
setAbool(boolean
abool)
{
this.abool
=
abool;
}
public
void
setAlong(long
along)
{
this.along
=
along;
}
public
void
setAfloat(float
afloat)
{
this.afloat
=
afloat;
}
public
void
setAdouble(double
adouble)
{
this.adouble
=
adouble;
}
public
void
setAstr(String
astr)
{
this.astr
=
astr;
}
public
void
setInts(int[]
ints)
{
this.ints
=
ints;
}
public
void
setStrs(String[]
strs)
{
this.strs
=
strs;
}
public
static
void
main(String[]
args)
{
Unit
a
=
new
Unit();
Unit
b
=
new
Unit();
short
ashort
=
1;
char
achar
=
'a';
byte
abyte
=
0x01;
boolean
abool
=
true;
long
along
=
100L;
float
afloat
=
3.14f;
double
adouble
=
6.28;
String
astr
=
"AHAN";
int[]
ints
=
{
1,
2,
3
};
String[]
strs
=
new
String[]
{
"A",
"B",
"C"
};
// 初始化Unit a
a.setAbool(abool);
a.setAbyte(abyte);
a.setAchar(achar);
a.setAdouble(adouble);
a.setAfloat(afloat);
a.setAlong(along);
a.setAshort(ashort);
a.setAstr(astr);
a.setInts(ints);
a.setStrs(strs);
// 初始化Unit b
b.setAbool(abool);
b.setAbyte(abyte);
b.setAchar(achar);
b.setAdouble(adouble);
b.setAfloat(afloat);
b.setAlong(along);
b.setAshort(ashort);
b.setAstr(astr);
int[]
ints_B
=
{
1,
3,
3
};
b.setInts(ints);
b.setStrs(strs);
System.out.println(a.equals(b));
System.out.println("a's
hashcode
is:
"
+
a.hashCode());
System.out.println("b's
hashcode
is:
"
+
b.hashCode());
}
}
输出如下:
true
a's hashcode is: -1285876460
b's hashcode is: -1285876460
首先,判断两个对象的hashCode是否相等;
如果不相等,认为两个对象也不相等;
如果相等,则判断两个对象用equals运算是否相等;
如果不相等,认为两个对象也不相等;
如果相等,认为两个对象相等。
当我们重写了一个类的equals()方法后,我们一定要记住同时也要重写hashCode()方法。
下面介绍如何来重写hashCode()方法。通常重写hashCode()方法按以下设计原则实现。
(1)把某个非零素数,例如17,保存在int型变量result中。
(2)对于对象中每一个关键域f(指equals方法中考虑的每一个域)参照以下原则处理。
boolean型,计算(f?0:1)。
byte、char和short型,计算(int)f。
long型,计算(int)(f^(f>>32))。
float型,计算Float.floatToIntBits(f)。
double型,计算Double.doubleToLongBits(f)得到一个long,再执行long型的处理。
对象引用,递归调用它的hashCode()方法。
数组域,对其中的每个元素调用它的hashCode()方法。
(3)将上面计算得到的散列码保存到int型变量c,然后执行result = 37 * result + c。
(4)返回result。
重写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
String
astr;
private
int[]
ints;
private
String[]
strs;
@Override
public
boolean
equals(Object
obj)
{
if
(obj
==
null)
{
return
false;
}
if
(!(obj
instanceof
Unit))
{
return
false;
}
Unit
o
=
(Unit)
obj;
return
o.ashort
==
ashort
&&
o.achar
==
achar
&&
o.abyte
==
abyte
&&
o.abool
==
abool
&&
o.along
==
along
&&
Float.floatToIntBits(o.afloat)
==
Float
.floatToIntBits(afloat)
&&
Double.doubleToRawLongBits(o.adouble)
==
Double
.doubleToRawLongBits(adouble)
&&
o.astr.equals(astr)
&&
Arrays.equals(ints,
o.ints)
&&
Arrays.equals(strs,
o.strs);
}
@Override
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.doubleToRawLongBits(adouble);
result
=
37
*
result
+
(int)
(tolong
^
(tolong
>>>
32));
result
=
37
*
result
+
astr.hashCode();
result
=
37
*
result
+
intsHashCode(ints);
result
=
37
*
result
+
unitsHashCode(strs);
return
result;
}
private
int
intsHashCode(int[]
aints)
{
int
result
=
17;
for
(int
i
:
aints)
{
result
=
37
*
result
+
i;
}
return
result;
}
private
int
unitsHashCode(String[]
astrs)
{
int
result
=
17;
for
(String
s
:
astrs)
{
result
=
37
*
result
+
s.hashCode();
}
return
result;
}
public
void
setAshort(short
ashort)
{
this.ashort
=
ashort;
}
public
void
setAchar(char
achar)
{
this.achar
=
achar;
}
public
void
setAbyte(byte
abyte)
{
this.abyte
=
abyte;
}
public
void
setAbool(boolean
abool)
{
this.abool
=
abool;
}
public
void
setAlong(long
along)
{
this.along
=
along;
}
public
void
setAfloat(float
afloat)
{
this.afloat
=
afloat;
}
public
void
setAdouble(double
adouble)
{
this.adouble
=
adouble;
}
public
void
setAstr(String
astr)
{
this.astr
=
astr;
}
public
void
setInts(int[]
ints)
{
this.ints
=
ints;
}
public
void
setStrs(String[]
strs)
{
this.strs
=
strs;
}
public
static
void
main(String[]
args)
{
Unit
a
=
new
Unit();
Unit
b
=
new
Unit();
short
ashort
=
1;
char
achar
=
'a';
byte
abyte
=
0x01;
boolean
abool
=
true;
long
along
=
100L;
float
afloat
=
3.14f;
double
adouble
=
6.28;
String
astr
=
"AHAN";
int[]
ints
=
{
1,
2,
3
};
String[]
strs
=
new
String[]
{
"A",
"B",
"C"
};
// 初始化Unit a
a.setAbool(abool);
a.setAbyte(abyte);
a.setAchar(achar);
a.setAdouble(adouble);
a.setAfloat(afloat);
a.setAlong(along);
a.setAshort(ashort);
a.setAstr(astr);
a.setInts(ints);
a.setStrs(strs);
// 初始化Unit b
b.setAbool(abool);
b.setAbyte(abyte);
b.setAchar(achar);
b.setAdouble(adouble);
b.setAfloat(afloat);
b.setAlong(along);
b.setAshort(ashort);
b.setAstr(astr);
int[]
ints_B
=
{
1,
3,
3
};
b.setInts(ints);
b.setStrs(strs);
System.out.println(a.equals(b));
System.out.println("a's
hashcode
is:
"
+
a.hashCode());
System.out.println("b's
hashcode
is:
"
+
b.hashCode());
}
}
输出如下:
true
a's hashcode is: -1285876460
b's hashcode is: -1285876460
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询