hashCode

简介

hashCode()获取哈希码(散列码),返回值为int。Object中hashCode的源码如下

/**
 * Returns a hash code value for the object. This method is
 * supported for the benefit of hash tables such as those provided by
 * {@link java.util.HashMap}.
 * 

* The general contract of {@code hashCode} is: *

    *
  • Whenever it is invoked on the same object more than once during * an execution of a Java application, the {@code hashCode} method * must consistently return the same integer, provided no information * used in {@code equals} comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. *
  • If two objects are equal according to the {@code equals(Object)} * method, then calling the {@code 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 {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code 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. *
*

* As much as is reasonably practical, the hashCode method defined by * class {@code Object} does return distinct integers for distinct * objects. (This is typically implemented by converting the internal * address of the object into an integer, but this implementation * technique is not required by the * Java™ programming language.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.lang.System#identityHashCode */ public native int hashCode();

英文简单翻译一下

hashCode常规协定:

a.在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。

b.如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。

c.以下情况不是必需的:如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashCode 方法必定会生成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。

d.实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 JavaTM 编程语言不需要这种实现技巧)

作用

产生的哈希码能确定对象在哈希表中的索引位置,散列表中存储的是K-V,能根据k定位v

代码测试

package cn.tedu.scalapackage;

import java.util.HashSet;

/**
 * @description: hash测试
 * @author: zfh
 * @email: [email protected]
 * @date: Created in 2021/6/11 22:46
 * @modified By:
 * @version: 1.0
 */
public class TestHash {
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public int hashCode() {
        return num % 100;
    }

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) {return true;}
//        if (o == null || getClass() != o.getClass()){ return false;}
//
//        TestHash testHash = (TestHash) o;
//
//        return num == testHash.num;
//    }

    public static void main(String[] args) {
        TestHash testHash1 = new TestHash();
        testHash1.setNum(1001);

        TestHash testHash2 = new TestHash();
        testHash2.setNum(1001);

        System.out.println(testHash1.hashCode() == testHash2.hashCode());
        System.out.println(testHash1.equals(testHash2));

        HashSet hashSet = new HashSet();
        hashSet.add(testHash1);
        hashSet.add(testHash2);
        System.out.println(hashSet.size());
    }
}

以上代码若是只重写hashCode方法,hash等equals不等说明是2个不同对象;若是放开equals注释,则hash等equals等说明是同一对象

你可能感兴趣的:(Java,java)