【Java总结】Hash算法和hashCode

本文深入探讨了Java中hashCode的作用,包括其在String类和HashMap中的应用。文章解释了为什么hashCode方法通常用于对象的唯一标识,以及为何在HashMap中使用位运算而非模运算。此外,还讨论了HashMap容量选择2的幂次方的原因,以确保更好的散列分布。最后,文章提到了HashMap容量设置的策略,以平衡空间和性能。
摘要由CSDN通过智能技术生成

既然打算长期写博客,就顺道把一些经典的问题归纳总结一下方便回顾和记忆,虽然做编程的不都是数学家,但是我们可以欣赏一下数学家在编程中创造的艺术

下面我们就带着一下几个问题去看源码

hashCode到底是干嘛用的

hashCode 方法是java.lang 包下的一个native方法,如果类不复写该方法的话,此方法原本返回的是该对象在内存在占用的空间地址,一般用此方法的返回值作为该对象唯一性的一个标识

/**
     * 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();

既然该方法可以被重写,因此其就不能作为获取对象的地址的一个绝对可靠的方法,如果需要获取对象的内存空间最好的方法是

System.identityHashCode(obj)

hashCode的使用场景

String类的使用

 /**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
   
        int h = hash;
        if (h == 0 && value
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值