简体   繁体   English

哈希图 <Integer, Object> 获取方法

[英]HashMap<Integer, Object> get method

I understand that the java.util.HashMap can only accept Objects as keys and thus cannot accept a primitive int but instead only accepts Integer objects. 我知道java.util.HashMap只能接受对象作为键,因此不能接受原始int,而只能接受Integer对象。

When using the get method to retrieve the value by using the key as a parameter must you put in an Integer object? 当使用get方法通过使用键作为参数来检索值时,是否必须放入Integer对象? Or will a primitive int suffice here? 还是一个原始int就足够了?

In Java 5+ a primitive int will be autoboxed into an Integer , so it's safe to use it; 在Java 5+中,原始int将自动装箱为Integer ,因此可以安全使用它。 prior to this you'll need to use an object 在此之前,您需要使用一个对象

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

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