相关疑难解决方法(0)

非空字符串的哈希码是否可以为零?

通过"非空",我的意思是在这个问题中包含至少一个非零字符的字符串.

作为参考,这是hashCode实现:

1493    public int hashCode() {
1494        int h = hash;
1495        if (h == 0) {
1496            int off = offset;
1497            char val[] = value;
1498            int len = count;
1499
1500            for (int i = 0; i < len; i++) {
1501                h = 31*h + val[off++];
1502            }
1503            hash = h;
1504        }
1505        return h;
1506    }
Run Code Online (Sandbox Code Playgroud)

并且算法在文档中指定.

在发生整数溢出之前,答案很简单:它不是.但我想知道的是,由于整数溢出,非空字符串的哈希码是否可能为零?你能建一个吗?

我正在寻找的理想情况是数学演示(或链接到一个)或构造算法.

java string hash integer-overflow

25
推荐指数
1
解决办法
3904
查看次数

标签 统计

hash ×1

integer-overflow ×1

java ×1

string ×1