JAVA基础——hashCode和equals的关系

equals

equals()源自于java.lang.Object,该方法用来简单验证两个对象的相等性。Object类中定义的默认实现只检查两个对象的对象引用,以验证它们的相等性。 通过重写该方法,可以自定义验证对象相等新的规则,如果使用ORM处理一些对象的话,要确保在hashCode()和equals()对象中使用getter和setter而不是直接引用成员变量

//equals源码
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;
    }

hashCode

hashCode()源自于java.lang.Object ,该方法用于获取给定对象的唯一的整数(散列码)。当这个对象需要存储在哈希表这样的数据结构时,这个整数用于确定桶的位置。默认情况下,对象的hashCode()方法返回对象所在内存地址的整数表示。hashCode()是HashTable、HashMap和HashSet使用的。默认的,Object类的hashCode()方法返回这个对象存储的内存地址的编号。

    //hashCode源码
    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和equals的关系

请看以下两个示例

示例一:

public static void main(String[] args) {
    String a = new String("123");
    String b = new String("123");
    System.out.println(a.hashCode()); //48690
    System.out.println(b.hashCode()); //48690
    System.out.println(a.equals(b));  //true
    System.out.println(a == b);  //false
}

示例二:

本示例中hashCode被重写。
    public static void main(String[] args) {
        class Object1{
            private final int hashCode = 21;
            public int hashCode(){
                return hashCode;
            }
        }
        class Object2{
            private final int hashCode = 21;
            public int hashCode(){
                return hashCode;
            }
        }
        Object1 object1 = new Object1();
        Object2 object2 = new Object2();
        Object1 object3 = new Object1();
        
        System.out.println(object1.hashCode()); //21
        System.out.println(object2.hashCode()); //21
        System.out.println(object3.hashCode()); //21
        System.out.println(object1.hashCode() == object2.hashCode()); //true
        System.out.println(object1.equals(object3)); //false
    }

通过以上两个示例,可以总结出以下4点:

  1. equals() 的作用是用来判断两个对象是否相等

  1. hashCode() 的作用是获取对象的哈希码;哈希码一般是一个整数,用来确定对象在哈希表中的索引位置。比如 HashMap本质上是通过数组实现的,当我们要获取某个“值”时,实际上是要获取数组中的某个位置的元素。而数组的位置,就是通过“键”来获取的;更进一步说,是通过“键”对应的哈希码计算得到的

  1. 如果两个对象需要相等(equals),那么它们必须有着相同的哈希码(hashCode)

  1. 但如果两个对象有着相同的哈希码,它们却不一定相等(equals方法被覆盖过,则hashCode方法也必须被覆盖。如果没有重写hashCode ,即使两个对象指向相同的数据,那么无论如何都不会相等)

以下为官方给出的关于 equals() 和 hashCode() 的解释:

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

猜你喜欢

转载自blog.csdn.net/DreamEhome/article/details/128817890
今日推荐