java 基础之(equals hashcode)

1.equals 的等价关系
[img]http://dl.iteye.com/upload/attachment/0062/2975/7a6bcb9c-ac2a-312b-9f32-7aa438fd9bd0.jpg[/img]

2. 重新equals 的具体方法

[img]http://dl.iteye.com/upload/attachment/0062/2977/52d98843-4efa-3420-a6bd-04dd426dbd2b.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0062/2979/bf1857b8-1f4d-3361-9275-cb32da91c678.jpg[/img]

3. 对应修改hashcode方法

[img]http://dl.iteye.com/upload/attachment/0062/2981/541dfee5-0f11-3ca0-ad95-4df2d43f99ad.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0062/2983/1f54b579-a768-3420-a866-84813fda77d6.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0062/2985/717c4469-e5e9-35bc-83df-f0b2401795a3.jpg[/img]


具体的例子

/**
*
*/
package com.study.effective;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
*
* @className :EqualsTest
* @package : com.study.effective
* @Description :effective Java 中关于equals的介绍
* @author:lgf
* @date :2012 一月 18 11:27:04
* @version : 1.0
*/
public class EqualsTest {
private int hashCode;
private String strValue;
private boolean booleanValue;
private long longValue;
private int intValue;
private double doubleValue;
private float floatValue;
private String [] arr;
private EqualsTest objField;

public EqualsTest(){

}

public EqualsTest(String strValue,int intValue){
this.strValue = strValue;
this.intValue = intValue;
}
/**
* 重写equals方法
* 对称,传递,一致
*/
@Override
public boolean equals(Object obj) {
//1. 判断是否是本身对象的引用
if (this == obj) {
return true;
}
//2. 判断是否就是本身对象的类型
if(!(obj instanceof EqualsTest)){
return false;
}

//3. 把参数转换成正确的类型,前面已经进行判断过了,所以肯定可以判断成功
EqualsTest temp = (EqualsTest)obj;

//4. 根据每个关键参数进行判断是否相等
// 就是你自己的判断依据

// return (this.strValue == temp.strValue)&&
// (this.booleanValue == temp.booleanValue)&&
// (this.intValue == temp.intValue) &&
// (Double.compare(this.doubleValue, temp.doubleValue) == 0)&&
// (Float.compare(this.floatValue, temp.floatValue) == 0) &&
// (Arrays.equals(this.arr, temp.arr))&&
// ((this.objField == temp.objField) || (this.objField != null && this.objField.equals(temp.objField)))

//4-1 基本数据类型(除了 double 和 float) 直接用 == 进行判断
if (this.strValue != temp.strValue) {
return false;
}

if (this.booleanValue != temp.booleanValue) {
return false;
}

if (this.longValue != temp.longValue) {
return false;
}

if (this.intValue != temp.intValue) {
return false;
}

//4-2double 类型数据可以通过Double.compare 进行判断 相等就== 0
if (Double.compare(this.doubleValue, temp.doubleValue) != 0) {
return false;
}

//4-3float 类型数据可以通过Float.compare 进行判断 相等就== 0
if (Float.compare(this.floatValue, temp.floatValue) != 0) {
return false;
}

//4-4 数组可以通过这个进行判断
if (!Arrays.equals(this.arr, temp.arr)) {
return false;
}

//4-5 如果是对象则可以通过这个进行判断
if(!((this.objField == temp.objField) || (this.objField != null && this.objField.equals(temp.objField)))){
return false;
}
return true;
}


/**
* 重写equals方法同时也要重写hashCode
* 每个对象的散列码
*/
@Override
public int hashCode() {
int result = hashCode;
if (result == 0) {
result = 1;
result = 31 * result + this.intValue; // byte,char, short, int 计算 (int)value
result = 31 * result + (this.booleanValue ? 1 : 0); // boolean 计算 (value ? 1 : 0)
result = 31 * result + (int)(this.longValue^(this.longValue>>>32));// long 计算 (value ^ (value>>>32))
result = 31 * result + Float.floatToIntBits(this.floatValue); // float 计算 (value ? 1 : 0)
result = 31 * result + (int)(Double.doubleToLongBits(this.doubleValue)^(Double.doubleToLongBits(this.doubleValue)>>>32));// 先转换成long 在转成int
result = 31 * result + Arrays.hashCode(this.arr); // 数组hashcode获得
result = 31 * result + (this.objField == null ? 0 : this.objField.hashCode());// 对象的hashcode获得
hashCode = result;
}
return hashCode;
}


public static void main(String[] args) {
EqualsTest e3 = new EqualsTest("e",0);
EqualsTest e4 = new EqualsTest("e",0);
//Map 把每个项相关的散列码缓存起来,如果散列码不匹配,就不必检查对象的相同性
Map<EqualsTest, String> map = new HashMap<EqualsTest, String>();
map.put(e3, "e3"); // 保存数据的时候就计算hashCode了
System.out.println(map.get(e4));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值