使用 HashMap 的时候,用 String 做 key 有什么好处?

在使用 HashMap 的时候,用 String 做 key 有什么好处?
HashMap 内部实现是通过 key 的 hashcode 来确定 value 的存储位置,因为字符串是不可变的,所以当创建字符串时,它的 hashcode 被缓存下来,不需要再次计算,所以相比于其他对象更快

posted @ 2022-06-01 22:56  大于昨天  阅读(73)  评论(0编辑  收藏  举报