Java中的hashCode和equals方法

在Java语言中,equals()hashCode()是Object类中的方法,这样,每一个类中都会继承Object类中这两个方法的默认实现。

1、说明

1.1 equals()方法

这个方法在Object类中的定义如下:

public boolean equals(Object obj) {
        return (this == obj);
}

如果要自己覆盖实现在这个方法,需要遵守如下原则:

  • For any object x, x.equals(x) should return true.
  • For any two object x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • For multiple objects x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • Multiple invocations of x.equals(y) should return same result, unless any of the object properties is modified that is being used in the equals() method implementation.
  • Object class equals() method implementation returns true only when both the references are pointing to same object.

1.2 hashCode()方法

这个方法是Object类的一个原生方法,它返回一个整数,代表这个对象的Hash值。
如果要自定义覆盖实现这个方法,需要注意遵守:

  • Multiple invocations of hashCode() should return the same integer value, unless the object property is modified that is being used in the equals() method.
  • An object hash code value can change in multiple executions of the same application.
  • If two objects are equal according to equals() method, then their hash code must be same.
  • If two objects are unequal according to equals() method, their hash code are not required to be different. Their hash code value may or may-not be equal.

1.3 equals()hashCode()使用约定

Java中基于Hash的数据结构在存储和检索数据的时候,会用到这两个方法。他们的实现,需要遵守:

  • If o1.equals(o2), then o1.hashCode() == o2.hashCode() should always be true.
  • If o1.hashCode() == o2.hashCode is true, it doesn’t mean that o1.equals(o2) will be true.

2、什么时候需要重写这两个方法

当重写equals()方法的时候,为了不违反前面的约定,基本一定要重写hashCode()方法。需要注意的是,即使违反了前面的约定,程序也不会抛出异常。如果这个类的对象没有用来作为Hash表的key值的话,没有问题。但是,如果这个类的对象被用来做为Hash表的key值,那么,必须要重新覆盖实现equals()和hashCode()方法,不然,如果依赖于默认的实现,会有问题。如下:
com/lfqy/trying/hashcodeequals/DataKey.java

package com.lfqy.trying.hashcodeequals;  
  
/**  
 * Created by chengxia on 2022/8/25. */public class DataKey {  
  
    private String name;  
    private int id;  
  
    // getter and setter methods  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    @Override  
    public String toString() {  
        return "DataKey [name=" + name + ", id=" + id + "]";  
    }  
  
}

com/lfqy/trying/hashcodeequals/HashingTest.java

package com.lfqy.trying.hashcodeequals;  
  
import java.util.HashMap;  
import java.util.Map;  
/**  
 * Created by chengxia on 2022/8/25. */public class HashingTest {  
  
    public static void main(String[] args) {  
        Map<DataKey, Integer> hm = getAllData();  
  
        DataKey dk = new DataKey();  
        dk.setId(1);  
        dk.setName("Paopao");  
        System.out.println(dk.hashCode());  
  
        Integer value = hm.get(dk);  
  
        System.out.println(value);  
  
    }  
  
    private static Map<DataKey, Integer> getAllData() {  
        Map<DataKey, Integer> hm = new HashMap<>();  
  
        DataKey dk = new DataKey();  
        dk.setId(1);  
        dk.setName("Paopao");  
        System.out.println(dk.hashCode());  
  
        hm.put(dk, 10);  
  
        return hm;  
    }  
  
}

运行后,输出如下:

1627674070
1360875712
null

Process finished with exit code 0

这里之所以最后取到的值是null,原因在于:在取值的时候,value = hm.get(dk)会先调用dk.hashCode()计算这个对象的HashCode,判断要取的值出现在哪个Hash桶,然后,调用equals方法,逐个检查该桶中的对象是否有返回true的,如果有就将这个对象返回,如果没有就返回null。从这个过程看,这里两个对象的hashCode值不一样,也不是一个对象,自然没法找到对应key的值,因此就返回null。

3、如何自定义覆盖实现equals()hashCode()方法

一般来说,IDE都支持自动生成这两个方法。需要注意的是,为了遵守前面的约定,这里在实现这两个方法时,一定让相同的对象属性参与计算。如下是一个IDEA自动生成的例子。
com/lfqy/trying/hashcodeequals/DataKey.java

package com.lfqy.trying.hashcodeequals;  
  
/**  
 * Created by chengxia on 2022/8/25. */public class DataKey {  
  
    private String name;  
    private int id;  
  
    // getter and setter methods  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getId() {  
        return id;  
    }  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
    @Override  
    public String toString() {  
        return "DataKey [name=" + name + ", id=" + id + "]";  
    }  
  
    @Override  
    public boolean equals(Object o) {  
        if (this == o) return true;  
        if (!(o instanceof DataKey)) return false;  
  
        DataKey dataKey = (DataKey) o;  
  
        if (id != dataKey.id) return false;  
        return name != null ? name.equals(dataKey.name) : dataKey.name == null;  
    }  
  
    @Override  
    public int hashCode() {  
        int result = name != null ? name.hashCode() : 0;  
        result = 31 * result + id;  
        return result;  
    }  
}

这样,再次运行,输出如下:

868822177
868822177
10

Process finished with exit code 0

4、Hash碰撞

Java语言中,Hash表结构的get和put函数,逻辑如下:

  1. First identify the “Bucket” to use using the “key” hash code.
  2. If there are no objects present in the bucket with same hash code, then add the object for put operation and return null for get operation.
  3. If there are other objects in the bucket with same hash code, then “key” equals method comes into play.
  • If equals() return true and it’s a put operation, then object value is overridden.
  • If equals() return false and it’s a put operation, then new entry is added to the bucket.
  • If equals() return true and it’s a get operation, then object value is returned.
  • If equals() return false and it’s a get operation, then null is returned.

两个对象有相同hashCode的现象,称为hash碰撞。如果hashCode函数实现有问题,就会出现较高概率的hash碰撞,进而导致键值分布不均,导致get和put操作出现效率问题。这就是为什么在生成hashCode使用了质数,主要就是为了让所有桶之间的分布比较均匀。

5、equals()hashCode()的最佳实践

  • Use same properties in both equals() and hashCode() method implementations, so that their contract doesn’t violate when any properties is updated.
  • It’s better to use immutable objects as Hash table key so that we can cache the hash code rather than calculating it on every call. That’s why String is a good candidate for Hash table key because it’s immutable and cache the hash code value.(字符串类型是常用的key值首选。)
  • Implement hashCode() method so that least number of hash collision occurs and entries are evenly distributed across all the buckets.

参考资料

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,517评论 6 539
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,087评论 3 423
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,521评论 0 382
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,493评论 1 316
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,207评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,603评论 1 325
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,624评论 3 444
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,813评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,364评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,110评论 3 356
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,305评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,874评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,532评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,953评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,209评论 1 291
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,033评论 3 396
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,268评论 2 375

推荐阅读更多精彩内容