Java:重写equals()和hashCode()
来源:互联网 发布:ps mac版破解 编辑:程序博客网 时间:2024/06/05 22:48
http://zhangjunhd.blog.51cto.com/113473/71571/
package com.demo.bean;
import java.util.Arrays;
public class Unit {
private byteabyte;
private shortashort;
private intaint;
private longalong;
private booleanabool;
private floatafloat;
private doubleadouble;
private charachar;
private Unit unit;
private int[]ints;
private Unit[] units;
/***
* [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。
*/
@Override
public int hashCode() {
int result = 17;
result = 37 * result + (int)abyte;
result = 37 * result + (int)ashort;
result = 37 * result + (int)aint;
result = 37 * result + (int)achar;
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 + unit.hashCode();
result = 37 * result + intsHashCode(ints);
result = 37 * result + unitsHashCode(units);
return result;
}
@Override
public boolean equals(Object object) {
// TODO Auto-generated method stub
if (!(object instanceof Unit)) {
returnfalse;
}
Unit uni = (Unit) object;
returnunit.abool == abool
&& uni.abyte ==abyte
&& uni.ashort ==ashort
&& uni.aint ==ashort
&& uni.along ==along
&& uni.afloat ==afloat
&& Float.floatToIntBits(uni.afloat) == Float
.floatToIntBits(afloat)
&& Double.doubleToLongBits(uni.adouble) == Double
.doubleToLongBits(adouble) && uni.unit.equals(unit)
&& uni.equalsInts(unit.ints) && uni.equalsUnits(units);
}
private boolean equalsInts(int[] aints) {
return Arrays.equals(ints, aints);
}
private boolean equalsUnits(Unit[] aUnits) {
return Arrays.equals(units, aUnits);
}
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;
}
}
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java: 重写equals()和hashCode()
- Java: 重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- java重写equals()和hashcode()
- Java重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- java:重写equals 和hashcode
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- Java:重写equals()和hashCode()
- 常见SQL性能优化
- centos 7.0 网卡配置及重命名教程(转)
- ios开发之如何从一个应用跳转到另一个应用(应用间跳转)
- Same Tree(LeetCode)
- Linux kernel编译错误
- Java:重写equals()和hashCode()
- 解决windows xp中不能使用gdiplus.dll 1.1的问题(vs2010中如何添加manifest文件)
- WPF Command 案例
- ps学习历程3
- 地图-大头针视图
- rowid format and the format of a row piece
- python 图论算法(一)
- G1垃圾回收器
- iOS开发工具集2