hashCode

本文解析了hashCode()在哈希表中的核心作用,介绍了hashCode方法的协定,并通过实例演示了如何重写hashCode与equals以影响对象在哈希表中的行为。重点讲解了对象散列原理与性能优化。
摘要由CSDN通过智能技术生成

简介

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}.
 * <p>
 * The general contract of {@code hashCode} is:
 * <ul>
 * <li>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.
 * <li>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.
 * <li>It is <em>not</em> 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.
 * </ul>
 * <p>
 * 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&trade; 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: hst1406959716@163.com
 * @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等说明是同一对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值