Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
put方法是用泛型定义的,而get对象是用对象定义的,为什么不get定义类似get(K key)?
put
get
get(K key)
put(K key, V value)
和
get(Object key)
集合中存在泛型类型以保持集合内容的一致性。即,如果你有一个Map<String, String>那么你不想以某种方式在它的某个地方得到一个整数。
Map<String, String>
get 方法不会造成这个问题,并且 get 使用对不一定必须是同一个类的键进行相等匹配。因此,一致性问题不需要使其通用,并且在针对键的相等匹配时是不可取的。