For the life of me I cannot understand why the compiler won't let me do the following...
import java.util.HashMap;
import java.util.Map;
public class TestMap {
private final Map<Integer, ? extends Number> map = new HashMap<Integer, Number>();
public void put(Integer key, Long item) {
this.map.put(key, item);
}
}
Why does line this.map.put(key, item)
cause a compile error?
I know that I can change the declaration of map to use Number
rather than ? extends Number
to make it work but it seems to me that what I am doing is perfectly legal, and I would prefer to not allow Number objects in the map. I am using Java 1.6.0_13.