Java String哈希码缓存 [英] Java String hashcode caching

查看:284
本文介绍了Java String哈希码缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String immutability的一个优点是哈希码缓存可以更快地访问。

One of the advantages of String immutability is hashcode caching for faster access.


  • 在这种情况下,如何处理具有相同哈希码的字符串的缓存?

  • 它真的吗?在这种情况下提高性能?

推荐答案


在这种情况下如何处理具有相同哈希码的字符串的缓存?

In this case how cache is handled for string which has same hashcode?

我不明白你问题的第一部分。无论哈希码是否相同,缓存都是针对所有字符串处理的(因为两个不同的字符串理论上可以具有相同的hashCode,因此如果hashCodes相等,则并不意味着字符串相等)。但是如果使用相同的String 对象,则不必重新计算hashCode,因为它是高速缓存的。

I don't understand the first part of your question. Cache is handled for all Strings the same, whether hashcodes are the same or not (since two different Strings can theoretically have the same hashCode, and so if hashCodes are equal, it doesn't mean that the Strings are equal). But if the same String object is used, the hashCode does not have to be recalculated since it is cached.


它真的能提高性能吗?

Does it really improve the performance?

明确地

Unequivocally YES

这篇关于Java String哈希码缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆