java HashMap<Integer,String> 它是如何取值的 <String,Integer>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38047298/
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
HashMap<Integer,String> how is it taking values <String,Integer>
提问by Piyush Sonavale
Here is my code, I wonder how is it possible:
这是我的代码,我想知道这怎么可能:
HashMap<Integer,String> hashmap = new HashMap();
hashmap.put(1,"milind");
hashmap.put(2,"nelay");
HashMap hash = new HashMap();
hash.put("piyush",1);
hashmap.putAll(hash);
for (Object name: hashmap.keySet()) {
Object key = name.toString();
Object value = hashmap.get(name);
System.out.println(key + " " + value);
}
Here is the output:
这是输出:
1 milind
2 nelay
piyush 1
回答by Nguyen Tuan Anh
Your hashmap
actually did not specify the types for Key/Value, so the Object type (or sub types including Integer, String and whatsoever)is acceptable for both key and value.
您hashmap
实际上没有指定键/值的类型,因此键和值都可以接受对象类型(或包括整数、字符串等在内的子类型)。
Here is your first line:
这是你的第一行:
HashMap hashmap = new HashMap();
If you change this line to:
如果您将此行更改为:
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
And continue with the next lines:
并继续下一行:
HashMap hash = new HashMap();
hash.put("piyush", 1);
hashmap.putAll(hash);
Then it won't compile.
然后它不会编译。
回答by Christoph Forster
Your HashMaps are not typesafe. The following will not compile anymore:
您的 HashMap 不是类型安全的。以下将不再编译:
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
hashmap.put(1, "milind");
hashmap.put(2, "nelay");
HashMap<String, Integer> hash = new HashMap<String, Integer>();
hash.put("piyush", 1);
hashmap.putAll(hash); // will not compile
for (Object name : hashmap.keySet()) {
Object key = name.toString();
Object value = hashmap.get(name);
System.out.println(key + " " + value);
}
回答by Tamas Rev
The generic type parameters, like <Integer, String>
add some compile-timechecking. Otherwise, the HashMapcan contain anything.
泛型类型参数,就像<Integer, String>
添加一些编译时检查一样。否则,HashMap可以包含任何内容。
Since the second map, HashMap hash=new HashMap();
has no type parameters, it passes the compiler check for void putAll(Map<? extends K,? extends V> m)
. Then, it can work well on runtime.
由于第二个映射HashMap hash=new HashMap();
没有类型参数,它通过了编译器检查void putAll(Map<? extends K,? extends V> m)
。然后,它可以在运行时很好地工作。
However, the caller of the map will have a verydifficult task to deal with Objects of unexpected type. This is how you can fix it on compiler level:
然而,地图的调用者将有一个非常困难的任务来处理意外类型的对象。这是在编译器级别修复它的方法:
private static void foo() {
HashMap<Integer,String> hashmap=new HashMap<>(); // diamond syntax to specify right-hand type
hashmap.put(1,"milind");
hashmap.put(2,"nelay");
HashMap<String, Integer> hash=new HashMap<>(); // diamond syntax again
hash.put("piyush",1);
hashmap.putAll(hash); // compile error
for (Object name: hashmap.keySet())
{
Object key =name.toString();
Object value = hashmap.get(name);
System.out.println(key + " " + value);
}
}