java:HashMap<String, int> 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1780385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java: HashMap<String, int> not working
提问by gklots
HashMap<String, int>
doesn't seem to work but HashMap<String, Integer>
does work.
Any ideas why?
HashMap<String, int>
似乎不起作用,但HashMap<String, Integer>
确实有效。任何想法为什么?
采纳答案by cletus
You can't use primitive types as generic arguments in Java. Use instead:
在 Java 中不能使用原始类型作为泛型参数。改用:
Map<String, Integer> myMap = new HashMap<String, Integer>();
With auto-boxing/unboxingthere is little difference in the code. Auto-boxing means you can write:
使用自动装箱/拆箱,代码几乎没有区别。自动装箱意味着您可以编写:
myMap.put("foo", 3);
instead of:
代替:
myMap.put("foo", new Integer(3));
Auto-boxing means the first version is implicitly converted to the second. Auto-unboxing means you can write:
自动装箱意味着第一个版本被隐式转换为第二个版本。自动拆箱意味着您可以编写:
int i = myMap.get("foo");
instead of:
代替:
int i = myMap.get("foo").intValue();
The implicit call to intValue()
means if the key isn't found it will generate a NullPointerException
, for example:
隐式调用intValue()
意味着如果找不到密钥,它将生成一个NullPointerException
,例如:
int i = myMap.get("bar"); // NullPointerException
The reason is type erasure. Unlike, say, in C# generic types aren't retained at runtime. They are just "syntactic sugar" for explicit casting to save you doing this:
原因是类型擦除。与 C# 中的泛型类型不同,在运行时不保留。它们只是显式转换的“语法糖”,以节省您执行此操作:
Integer i = (Integer)myMap.get("foo");
To give you an example, this code is perfectly legal:
举个例子,这段代码是完全合法的:
Map<String, Integer> myMap = new HashMap<String, Integer>();
Map<Integer, String> map2 = (Map<Integer, String>)myMap;
map2.put(3, "foo");
回答by Peter Lawrey
GNU Trove support this but not using generics. http://trove4j.sourceforge.net/javadocs/gnu/trove/TObjectIntHashMap.html
GNU Trove 支持这一点,但不使用泛型。http://trove4j.sourceforge.net/javadocs/gnu/trove/TObjectIntHashMap.html
回答by Chitrapal Singh
You can use reference type in generic arguments, not primitive type. So here you should use
您可以在泛型参数中使用引用类型,而不是原始类型。所以在这里你应该使用
Map<String, Integer> myMap = new HashMap<String, Integer>();
and store value as
并将值存储为
myMap.put("abc", 5);
回答by Chitrapal Singh
You cannot use primitive types in HashMap
. int
, or double
don't work. You have to use its enclosing type. for an example
您不能在HashMap
. int
,或者double
不工作。你必须使用它的封闭类型。举个例子
Map<String,Integer> m = new HashMap<String,Integer>();
Now both are objects, so this will work.
现在两者都是对象,所以这将起作用。
回答by Younes Meridji
int is a primitive type, you can read what does mean a primitive type in java here, and a Map is an interface that has to objects as input:
int 是一种原始类型,您可以在这里阅读 java 中的原始类型的含义,而 Map 是一个接口,必须将对象作为输入:
public interface Map<K extends Object, V extends Object>
object means a class, and it means also that you can create an other class that exends from it, but you can not create a class that exends from int. So you can not use int variable as an object. I have tow solutions for your problem:
object 意味着一个类,这也意味着您可以创建一个从它扩展的其他类,但您不能创建一个从 int 扩展的类。所以你不能使用 int 变量作为对象。对于您的问题,我有两个解决方案:
Map<String, Integer> map = new HashMap<>();
or
或者
Map<String, int[]> map = new HashMap<>();
int x = 1;
//put x in map
int[] x_ = new int[]{x};
map.put("x", x_);
//get the value of x
int y = map.get("x")[0];