为什么要重写hashCode() 和 equals()

equals()

默认的Object类里面equals()方法是根据对象所在的内存来做判断的,如果两个对象引用指向的是同一个内存,则返回true,但是,在某些场景一下,我们不想这么苛刻,比如是String类的equals(),只是判断了String.value的值,String其它的属性是不参与判断的,所以我们比较字符串的时候只是比较其中的内容,下面是String的equals()方法和hashCode()方法。

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
} public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value; for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}

hashCode()

一般而言,默认的hashCode()方法会返回一个与对象的内存地址相关的值,有时候直接就是内存地址;所以,如何两个对象的hashCode()不相等,则它们肯定是不相等;但反过来,如果两个对象的hashCode()相等,它们不一定是相等的。

如果我们创建了属于自己的Class,还使用默认的hashCode()方法,那就可以出错,特别是在使用该Class作为HashMap的Key的时候。

重写hashCode()的基本原则

该基本原则是参考《Effective Java》Joshua Bloch's recipe

A、初始化一个整形变量,为此变量赋予一个非零的常数值,比如int result = 17;
B、选取equals方法中用于比较的所有域(之所以只选择equals()中使用的域,是为了保证上述原则的第1条),然后针对每个域的属性进行计算:
(1) boolean,c = f ? 1:0
(2) byte\char\short\int,  c= (int)f
(3) long,c = (int)(f ^ (f >>> 32))
(4) float,c = Float.floatToIntBits(f)
(5) double,long l = Double.doubleToLongBits(f),c = (int)(l ^ (l >>> 32))
(6) Object,对里面的属性采用上面同一的方法来判断
(7) 如果是数组,那么需要为每个元素当做单独的域来处理。java.util.Arrays.hashCode方法包含了8种基本类型数组和引用数组的hashCode计算,算法同上。
(8)、最后,把每个域的散列码合并到对象的哈希码中。

一个具体例子

public class Person {

    private String name;

    private byte[] password ;

    private String nickname ; // ignore the nickname

    private int age ;

    private double height ;

    private long birthYear ;

    private float sex ;

    private boolean alive;

    public Person(String name, byte[] password, int age, double height, long birthYear, float sex, boolean alive , String nickname) {
this.name = name;
this.password = password;
this.nickname = nickname;
this.age = age;
this.height = height;
this.birthYear = birthYear;
this.sex = sex;
this.alive = alive;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public byte[] getPassword() {
return password;
} public void setPassword(byte[] password) {
this.password = password;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getHeight() {
return height;
} public void setHeight(double height) {
this.height = height;
} public long getBirthYear() {
return birthYear;
} public void setBirthYear(long birthYear) {
this.birthYear = birthYear;
} public float getSex() {
return sex;
} public void setSex(float sex) {
this.sex = sex;
} public boolean isAlive() {
return alive;
} public void setAlive(boolean alive) {
this.alive = alive;
} /**
* Joshua Bloch's recipe
* @return int
*/
@Override
public int hashCode() {
int result = 17 ;
result = 37 * result + name.hashCode();
result = 37 * result + Arrays.hashCode(password);
result = 37 * result + age;
long l = Double.doubleToLongBits(height);
result = 37 * result + (int)(l ^ (l >>> 32));
result = 37 * result + (int)(birthYear ^ (birthYear >>> 32));
result = 37 * result + Float.floatToIntBits(sex);
result = 37 * result + (alive ? 0 : 1 );
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (obj instanceof Person){
Person anotherPerson = (Person)obj;
if (this.getName().equals(anotherPerson.getName())
&& Arrays.equals(this.getPassword(),anotherPerson.getPassword())
&& this.age == anotherPerson.getAge()
&& this.height == anotherPerson.getHeight()
&& this.birthYear == anotherPerson.getBirthYear()
&& this.sex == anotherPerson.getSex()
&& this.alive == anotherPerson.isAlive()
){
return true;
}else {
return false;
}
}
return false;
}
} public class Main { public static void main(String[] args) {
String name = "Tom";
byte [] password = "123".getBytes();
int age = 18 ;
double height = 3.14;
long birthYear = 2000 ;
float sex = 1 ;
boolean alive = true ;
Person Tom = new Person(name,password,age,height,birthYear,sex,alive,"Tom");
Person Deal = new Person(name,password,13,height,birthYear,sex,alive,"Deal");
Person Jack = new Person(name,password,age,height,birthYear,sex,alive,"Jack"); // map是根据hashcode来判断的,然后还有疑惑,尝试把 Person.hashcode函数注释再运行程序,你会明白
Map<Person,String> personStringMap = new HashMap<Person, String>(10);
personStringMap.put(Tom,"Tom");
personStringMap.put(Deal,"Deal"); System.out.println("Tom's nickname is : "+personStringMap.get(Tom));
System.out.println("Jack's nickname is : "+personStringMap.get(Jack));
System.out.println("Deal's nickname is : "+personStringMap.get(Deal));
System.out.println("is Tom equals to Jack : "+Tom.equals(Jack)); // list 的contains 是根据equal方法来判断的
List<Person> persons = new ArrayList<Person>();
persons.add(Tom);
persons.add(Deal);
System.out.println("persons contains Jack: " + persons.contains(Jack));
System.out.println("persons contains Deal: " + persons.contains(Deal));
}
}

参考 http://www.cnblogs.com/kismetv/p/7191736.html

参考 https://www.cnblogs.com/dolphin0520/p/3681042.html

Java 重写hashCode()与equals()的更多相关文章

  1. Java 重写 hashCode() 和 equals() 方法

    1. hashCode 1.1 基本概念 hashCode 是 JDK 根据对象的地址算出来的一个 int 数字(对象的哈希码值),代表了该对象再内存中的存储位置. hashCode() 方法是超级类 ...

  2. 【java编程】重写HashCode和equals方法

    [一]重写equals方案的规则 equals方法本来的原则 1.类的每个实例本质上都是唯一的. 2.不关心类是否提供了“逻辑相等”的测试功能 3.超类已经覆盖了equals,从超类继承过来的行为对于 ...

  3. java中hashcode和equals的区别和联系

    HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...

  4. 为什么要重写hashcode和equals方法?初级程序员在面试中很少能说清楚。

    我在面试 Java初级开发的时候,经常会问:你有没有重写过hashcode方法?不少候选人直接说没写过.我就想,或许真的没写过,于是就再通过一个问题确认:你在用HashMap的时候,键(Key)部分, ...

  5. (转)为什么要重写 hashcode 和 equals 方法?

    作者丨hsm_computer cnblogs.com/JavaArchitect/p/10474448.html 我在面试Java初级开发的时候,经常会问:你有没有重写过hashcode方法?不少候 ...

  6. 深入探究Java中hashCode()和equals()的关系

    目录 一.基础:hashCode() 和 equals() 简介 equals() hashCode() 二. 漫谈:初识 hashCode() 与 equals() 之间的关系 三. 解密:深入理解 ...

  7. HashMap中使用自定义类作为Key时,为何要重写HashCode和Equals方法

    之前一直不是很理解为什么要重写HashCode和Equals方法,才只能作为键值存储在HashMap中.通过下文,可以一探究竟. 首先,如果我们直接用以下的Person类作为键,存入HashMap中, ...

  8. 面试题:我们重写一个对象的时候为什么要同时重写hashcode()和equals()方法

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 在创建的类不重写hashCode()和equals() 方法时,默认使用 java 提供的 java.l ...

  9. Java中HashCode()和equals()的作用

    引言 我们知道Java中的集合(Collection)大致可以分为两类,一类是List,再有一类是Set. 前者集合内的元素是有序的,元素可以重复:后者元素无序,但元素不可重复. 这里就引出一个问题: ...

随机推荐

  1. 浅谈进程&线程

    学习过操作系统(下面简称OS)的都清楚,计算机计算的核心是CPU,操作系统是计算机资源的管理者 同事也是软硬件之间的接口.为了实现程序的并发,而引入了进程的概念.在传统OS中,进程是个很重要的概念,它 ...

  2. Cocos Creator 通用框架设计 —— 资源管理优化

    接着<Cocos Creator 通用框架设计 -- 资源管理>聊聊资源管理框架后续的一些优化: 通过论坛和github的issue,收到了很多优化或bug的反馈,基本上抽空全部处理了,大 ...

  3. 解决pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.问题

    国内的其他镜像源清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技 ...

  4. vscode jshint 报'import' is only available in ES6 (use 'esversion: 6'). (W119)错误

    vue项目用vscode打开代码前出现黄点,js报错 'import' is only available in ES6 (use 'esversion: 6'). (W119) 意思是import属 ...

  5. jchdl-GSL-实例 - 使用Intellij IDEA创建Mux

    https://mp.weixin.qq.com/s/yP9xKeg0iHJChuMPzxdJtA https://github.com/wjcdx/jchdl/blob/master/src/org ...

  6. Java实现 蓝桥杯 算法训练 审美课

    算法训练 审美课 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 <审美的历程>课上有n位学生,帅老师展示了m幅画,其中有些是梵高的作品,另外的都出自五岁小朋友之手.老师 ...

  7. Java实现 LeetCode 733 图像渲染(DFS)

    733. 图像渲染 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的 ...

  8. Java实现 蓝桥杯VIP 算法提高 11-2删除重复元素

    算法提高 11-2删除重复元素 时间限制:10.0s 内存限制:256.0MB 问题描述 为库设计新函数DelPack,删除输入字符串中所有的重复元素.不连续的重复元素也要删除. 要求写成函数,函数内 ...

  9. Java实现回文判断

    1 问题描述 给定一个字符串,如何判断这个字符串是否是回文串? 所谓回文串,是指正读和反读都一样的字符串,如madam.我爱我等. 2 解决方案 解决上述问题,有两种方法可供参考: (1)从字符串两头 ...

  10. 详解 Flink DataStream中min(),minBy(),max(),max()之间的区别

    解释 官方文档中: The difference between min and minBy is that min returns the minimum value, whereas minBy ...