Java 6 Map.get()类型安全意外行为(?) [英] Java 6 Map.get() Type Safety Unexpected Behavior(?)
问题描述
可能存在重复:
为什么Map.get(对象键)不是(完全)通用的原因
根据javadocs( http://java.sun.com/javase/6/docs/api/java/util/Map.html )对于Map接口,get的定义是
V get(Object key)
返回指定键映射到的值,或者如果此映射包含null
没有为
键的映射。
代码示例:
Map< InstrumentInfo,Double> moo = new HashMap< InstrumentInfo,Double>();
moo.get(new Integer(5));
我期望上面的代码会抛出异常或者至少发出警告。
我认为在泛型和类型安全性方面,get方法会带入一个类型参数。在这个问题中讨论 stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic\">为什么Map.get(Object key)不是(完全)通用的
Possible Duplicate:
What are the reasons why Map.get(Object key) is not (fully) generic
According to the javadocs (http://java.sun.com/javase/6/docs/api/java/util/Map.html) for the Map interface, the definition of get is
V get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
Code Example:
Map<InstrumentInfo, Double> moo = new HashMap<InstrumentInfo,Double>();
moo.get(new Integer(5));
I would expect that the above code will throw an exception or at least give a warning.
I would expect that with generics and type safety, the get method would take in a parameter of type . What is the reason for taking in type Object and not ?
Discussed in this question What are the reasons why Map.get(Object key) is not (fully) generic, as well...
这篇关于Java 6 Map.get()类型安全意外行为(?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!