`
编程足球
  • 浏览: 251227 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

java 基础之(equals hashcode)

    博客分类:
  • java
 
阅读更多
1.equals 的等价关系


2. 重新equals 的具体方法





3. 对应修改hashcode方法








具体的例子
/**
 * 
 */
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));
		
	}
	
}

  • 大小: 66.8 KB
  • 大小: 257.1 KB
  • 大小: 123.7 KB
  • 大小: 101.6 KB
  • 大小: 276.3 KB
  • 大小: 178.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics