先看下方法介绍
按照IEEE 754标准,32位浮点数在计算机中二进制存储形式共三部分:S(1位,符号) E(8位,阶码) M(23位,尾数)
给你举个例子吧,Float.floatToIntBits(20.5f)按照如下方式计算:
20.59D=10100.1B=1.01001*2^4B 指数e=4
S=0--> 正数 E=4+127=131D=10000011B--> 真实指数e变成阶码E时需加127 M=01001B
则32位2进制存储形式为:0 10000011 01001000000000000000000
转换成10进制即1101266944
给你举个例子吧,Float.floatToIntBits(20.5f)按照如下方式计算:
20.59D=10100.1B=1.01001*2^4B 指数e=4
S=0--> 正数 E=4+127=131D=10000011B--> 真实指数e变成阶码E时需加127 M=01001B
则32位2进制存储形式为:0 10000011 01001000000000000000000
转换成10进制即1101266944
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()。
- 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;
- }
- }
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;
}
}
6.看看eclipse自动生成的equals和hashcode方法,还是很好的
这个是我自己补充:
- public class A {
- String a;
- int b=0;
- String[]c;
- float d;
- A a1;
- A[] a2;
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((a == null) ? 0 : a.hashCode());
- result = prime * result + ((a1 == null) ? 0 : a1.hashCode());
- result = prime * result + Arrays.hashCode(a2);
- result = prime * result + b;
- result = prime * result + Arrays.hashCode(c);
- result = prime * result + Float.floatToIntBits(d);
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- A other = (A) obj;
- if (a == null) {
- if (other.a != null)
- return false;
- } else if (!a.equals(other.a))
- return false;
- if (a1 == null) {
- if (other.a1 != null)
- return false;
- } else if (!a1.equals(other.a1))
- return false;
- if (!Arrays.equals(a2, other.a2))
- return false;
- if (b != other.b)
- return false;
- if (!Arrays.equals(c, other.c))
- return false;
- if (Float.floatToIntBits(d) != Float.floatToIntBits(other.d))
- return false;
- return true;
- }
- }
public class A {
String a;
int b=0;
String[]c;
float d;
A a1;
A[] a2;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((a == null) ? 0 : a.hashCode());
result = prime * result + ((a1 == null) ? 0 : a1.hashCode());
result = prime * result + Arrays.hashCode(a2);
result = prime * result + b;
result = prime * result + Arrays.hashCode(c);
result = prime * result + Float.floatToIntBits(d);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (a == null) {
if (other.a != null)
return false;
} else if (!a.equals(other.a))
return false;
if (a1 == null) {
if (other.a1 != null)
return false;
} else if (!a1.equals(other.a1))
return false;
if (!Arrays.equals(a2, other.a2))
return false;
if (b != other.b)
return false;
if (!Arrays.equals(c, other.c))
return false;
if (Float.floatToIntBits(d) != Float.floatToIntBits(other.d))
return false;
return true;
}
}
这里可以看到它没有能自动实现一个类里面有属于自己类型的属性的hashcode,结合上面标题5的思想设计,就比较完美了,但这个equals()方法的头几步判断是比标题5里面的好的。