在 HashMap 中存储具有原始 int 的 Integer 对象

发布于 2024-09-19 13:15:43 字数 143 浏览 4 评论 0原文

HashMap 使用对象作为键。如果您使用 int 基元作为键,它会使用自动装箱并为键创建整数对象。

是否有任何 hashmap 实现使用原始类型作为键。我不想自动装箱。因为整数的 hascode 也是整数的值。

我正在尝试创建整数对象池。

HashMap uses objects as a key. If you use int primitive as key, it uses auto boxing and create integer objects for key.

is there any hashmap implementation uses primitive types as key. I dont want autoboxing. becuase hascode of integer is also value of integer.

I am trying to create integer object pool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2024-09-26 13:15:43

是的,您可以使用 trove 库。它提供 int -> object (和 int -> int/float/...)哈希映射,直接使用原始类型,无需自动装箱。但你确定值得吗?自动装箱速度很快,而且 jit 编译器可以非常有效地优化代码。

Yes, you can use the trove library. It provides int -> object (and int -> int/float/...) hash map, directly with primitive types without autoboxing. But are you sure it is worth it? Autoboxing is fast and the jit compiler optimizes the code quite efficiently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文