为什么哈希图中的不可变对象如此有效?

为什么哈希图中的不可变对象如此有效?,第1张

为什么哈希图中的不可变对象如此有效?

String#hashCode

private int hash;...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;}

由于

String
永不更改的内容,因此该类的创建者选择在对哈希进行一次计算之后就对其进行缓存。这样,不会浪费时间重新计算相同的值。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5091565.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存