This question already has an answer here:
这个问题在这里已有答案:
- What are the reasons why Map.get(Object key) is not (fully) generic 11 answers
Map.get(Object key)不是(完全)通用11答案的原因是什么?
HashMap<Integer, String> hashMap = new HashMap<>();
hashMap.put(1, "aaa");
hashMap.put(2, "baa");
hashMap.put(3, "caa");
System.out.println(hashMap.get(false));
Above code compiles and runs fine.. gives output as null
.
上面的代码编译并运行正常..给出输出为null。
What I am trying to understand is there any autoboxing happening in-between that i seem to miss. Because if generics are applied at compile time, get method shall not allow us to pass a boolean there.
我想要了解的是,我之间似乎错过了任何自动装箱。因为如果在编译时应用泛型,get方法不允许我们在那里传递布尔值。
Thanks
4 个解决方案
#1
2
The get
method of HashMap is defined as public V get(Object arg0) {
. That means you can put any object as parameter. That method do not use generics, so the Parameter is not checked by the Compiler.
HashMap的get方法定义为public V get(Object arg0){。这意味着您可以将任何对象作为参数。该方法不使用泛型,因此编译器不会检查参数。
Here you can find the javadoc.
在这里你可以找到javadoc。
#2
0
The source code of the mao imnplementation is taking Object as parameter, so you basically can pass as param whatever you want...
mao imnplementation的源代码是将Object作为参数,所以你基本上可以像你想要的那样传递param ...
V get(Object key);
and the HashMap
和HashMap
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
so if you give another instance as parameter the method will return null
因此,如果您将另一个实例作为参数,则该方法将返回null
#3
0
The signature of get
method in public class HashMap<K,V>
is
公共类HashMap
get(Object key)
It isn't generic. So it's not just boolean
, you can pass any object and it will give some output (or null
if it doesn't exists).
它不通用。所以它不仅仅是布尔值,你可以传递任何对象,它会给出一些输出(如果它不存在则为null)。
I think you're confusing this signature with
我认为你这个签名混淆了
get(K key)
If it was a signature of this kind, then your operation wouldn't be allowed.
如果是这种签名,则不允许您的操作。
#4
0
Map.get
isn't a generic method. That way it maintains comparability with pre-generics Java. If it were generic, to maintain compatibility would require dropping the old method (because all types would match Object
causing ambiguity for the compiler).
Map.get不是通用方法。这样它就保持了与前仿制药Java的可比性。如果它是通用的,保持兼容性将需要删除旧方法(因为所有类型都匹配Object导致编译器的歧义)。
#1
2
The get
method of HashMap is defined as public V get(Object arg0) {
. That means you can put any object as parameter. That method do not use generics, so the Parameter is not checked by the Compiler.
HashMap的get方法定义为public V get(Object arg0){。这意味着您可以将任何对象作为参数。该方法不使用泛型,因此编译器不会检查参数。
Here you can find the javadoc.
在这里你可以找到javadoc。
#2
0
The source code of the mao imnplementation is taking Object as parameter, so you basically can pass as param whatever you want...
mao imnplementation的源代码是将Object作为参数,所以你基本上可以像你想要的那样传递param ...
V get(Object key);
and the HashMap
和HashMap
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
so if you give another instance as parameter the method will return null
因此,如果您将另一个实例作为参数,则该方法将返回null
#3
0
The signature of get
method in public class HashMap<K,V>
is
公共类HashMap
get(Object key)
It isn't generic. So it's not just boolean
, you can pass any object and it will give some output (or null
if it doesn't exists).
它不通用。所以它不仅仅是布尔值,你可以传递任何对象,它会给出一些输出(如果它不存在则为null)。
I think you're confusing this signature with
我认为你这个签名混淆了
get(K key)
If it was a signature of this kind, then your operation wouldn't be allowed.
如果是这种签名,则不允许您的操作。
#4
0
Map.get
isn't a generic method. That way it maintains comparability with pre-generics Java. If it were generic, to maintain compatibility would require dropping the old method (because all types would match Object
causing ambiguity for the compiler).
Map.get不是通用方法。这样它就保持了与前仿制药Java的可比性。如果它是通用的,保持兼容性将需要删除旧方法(因为所有类型都匹配Object导致编译器的歧义)。