Java 中 equals() 和 hashcode() 方法详解

时间:2021-08-23 14:41:39   收藏:0   阅读:23

hashCode 使用中产生的问题

HashSet 是一个无序、不可重复的集合,代码如下:

public class HashSetDemo {
    public static void main(String[] args) {
        Set<Student> sets = new HashSet<>();
        sets.add(new Student(1,"张三",23));
        sets.add(new Student(2,"李四",24));
        sets.add(new Student(3,"王五",22));
        sets.add(new Student(4,"赵六",27));
        sets.add(new Student(4,"赵六",27));

        for (Student student: sets) {
            System.out.println(student);
        }
    }
}

运行打印结果如下:

Student{id=4, name=‘赵六‘, age=27, clazz=‘null‘, score=0.0}
Student{id=3, name=‘王五‘, age=22, clazz=‘null‘, score=0.0}
Student{id=4, name=‘赵六‘, age=27, clazz=‘null‘, score=0.0}
Student{id=1, name=‘张三‘, age=23, clazz=‘null‘, score=0.0}
Student{id=2, name=‘李四‘, age=24, clazz=‘null‘, score=0.0}

是不是结果和我们想象的不一样,HashSet 集合元素不是不重复,为何这里却发生的重复现象呀?这其实是因为 equals、hashCode 使用不规范导致的。那么,equals 和 hashCode 到底有何关系呢?为何影响 HashSet 的使用?

equals 与 Hashcode 的关系

它们都是 Object 类中的方法,如下:

public boolean equals(Object obj)
public int hashCode()

阅读 hashCode 方法的注释如下:

  1. hashCode() 方法返回对象的哈希码,支持该方法是为哈希表提供一些优点,例如,HashMap 提供的哈希表。
  2. 同一个对象未发生改变时多次调用 hashCode() 返回值必须相同
  3. 两个对象 equals 不相等,那么两对象的 hashCode() 返回必定不同(此处可用来提高哈希表性能)
  4. 两个对象的 hashCode() 返回值相同,两对象不一定相同,还需要通过 equals() 再次判断
  5. 当 equals 方法被重写时,通常有必要重写 hashCode 方法

通过第 1 点其实可以看出,hashCode() 在散列表中才有用,在其它情况下没用。在散列表中 hashCode() 的作用是获取对象的散列码,进而确定该对象在散列表中的位置,当对象不会用来创建像 hashMap、hashSet 等散列表时,hashCode() 实际上用不上。

产生问题的原因

分析原因前需要了解哈希表的底层实现,hashCode 在哈希表中充当的作用:

其实在 HashSet 就是采用的这种存储和获取方式,通过 HashCode 和 equals 组合的方式来保证集合无重复。也说明了 HashCode() 在散列表中是发挥作用的

hashCode 的正确使用

从 JDK 源码的注释中可以看出,hashCode() 在散列表中才会发挥作用,当对象无需创建像 HashMap、HashSet 等集合时,可以不用重写 hashCode() 方法,但是如果有使用到对象的哈希集合等操作时,必须重写 hashCode() 和 equals()。

原文:https://www.cnblogs.com/binbingg/p/14450595.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!