map的get方法定义如下:
V get(Object key)
Map是使用泛型的K,V作为键值类型的,那么为什么get的参数不是泛型而是Object类型呢?
同样的道理适用于 java.util.Collection.remove.boolean
remove(Object o)
参考
- http://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic
- http://www.jianshu.com/p/7889e4b32362
这一篇文章表示如果采用范型作为get的参数并不能很好的体现出map的精神,因为key是否相等,取决于key1.equals(key2)是否成立,而并不应该限制类型。另外一篇文章也阐述了这个问题,并引用了Josh Bloch的一段话:
Josh Bloch says (6:41) that they attempted to generify the get method of Map, remove method and some other, but “it simply didn’t work”. There are too many reasonable programs that could not be generified if you only allow the generic type of the collection as parameter type. The example given by him is an intersection of a List of Numbers and a List of Longs.
大概意思就是说我们曾经尝试过把get方法范型化,但是它挂了,有很多很多原因使他不能被范型化,比如你用List<Number>做key,但却想用List<Long>来get。
重新整理
先看一下map的get方法定义
1 | V get(Object key); |
自从jdk1.5引入泛型之后,map的定义都是
1 | public interface Map<K,V> |
k是键的类型,v是值类型。所以在自然而然的认为map的get至少是这样的V get(K key),但事实竟然不是。。。
针对这点疑问网上也是有各种声音,下面这个说的比较合理
意思是map的get思想是只要key相等equal,那么就可以获取到对应的值
1 | final Node<K,V> getNode(int hash, Object key) { |
所以并不需要类型相等。比如下面这个例子:
1 | Map<List<String>,String> map = new HashMap<>(); |
如果强制类型,反而违背了map.get的实现实质。
补充
Collection接口的contains和remove等,也是如此。