从String源码揭秘hashcode()算法

本文详细解析了Java中String类的hashCode()方法实现原理,包括其内部变量的作用及hash值计算过程,并通过实例演示了如何计算特定字符串的hash值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看下java源码中对hashcode()方法里面用到的变量的声明。

 /** The value is used for character storage. */
    private final char value[];//定义一个字符数组value,用于存储字符串里面的字符

    /** The offset is the first index of the storage that is used. */
    private final int offset;//定义offset变量表示字符串第一个字符的下标索引

    /** The count is the number of characters in the String. */
    private final int count;//定义count变量表示字符串中字符的个数

    /** Cache the hash code for the string */
    private int hash; // Default to 0,默认值为0

下面就是hashcode()方法的源码

/**
  * Returns a hash code for this string. The hash code for a
  * <code>String</code> object is computed as
  * <blockquote><pre>
  * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
  * </pre></blockquote>
  * using <code>int</code> arithmetic, where <code>s[i]</code> is the
  * <i>i</i>th character of the string, <code>n</code> is the length of
  * the string, and <code>^</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 && count > 0) {
      int off = offset;
      char val[] = value;
      int len = count;


      for (int i = 0; i < len; i++) {
        h = 31*h + val[off++];
      }
      hash = h;
    }
    return h;
  }


下面以cat为例。根据hashcode()源码中的循环部分的算法,计算一下cat的hashcode值

首先,c,a,t 的ascii码值分别为99,97,116;字符个数3,所以循环部分的代码变成如下所示

public int hashCode() {
        int h = hash;
        if (h == 0 && count > 0) {
            int off = 0;
            char val[] = value;
            int len = 3;

            for (int i = 0; i < 3; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

也就是总共3次循环

第一次循环:h1=31*0+val[0]=val[0]='c'=99

第二次循环:h2=31*h1+val[1]=31*99+'a'=31*99+97=3166

第三次循环:h3=31*h2+val[2]=31*3166+'t'=31*3166+116=98262

所以cat的hashcode值为98262

再用多项式的表示方法检验一下s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

99*31的平方+97*31+116结果也等于98262

所以计算正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值