简体   繁体   English

为什么我不能在Java中使用'long'类型创建HashMap?

[英]Why can I not create a HashMap with 'long' types in Java?

有什么理由不允许以下情况?

HashMap<long, long> x = new HashMap<>();

你正在使用原语重写为HashMap<Long,Long> x = new HashMap<>()

In Java, types with generic type parameters, such as HashMap, only accept types that inherit from Object. 在Java中,具有泛型类型参数的类型(例如HashMap)仅接受从Object继承的类型。 long does not inherit from Object, so you can't use it with HashMap. long不从Object继承,因此您不能将它与HashMap一起使用。 You can however use Long, which is a boxed version of long. 但是你可以使用Long,它是long的盒装版本。

Using standard collections for primitive types like long is not really effective 对像long这样的原始类型使用标准集合并不是很有效

If you need to minimize memory footprint and get better performance you should consider 3rd-party collection libraries like Trove 如果您需要最小化内存占用并获得更好的性能,您应该考虑像Trove这样的第三方收集库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM