How can i add an MyObj
to this aMap
?
You can't, because of the upper bound on the key type.
The reason is that ? extends A
could be MyOtherObj implements A
. In this case, it would be type-unsafe to be able to put a key of type MyObj
into the map:
Map<MyOtherObj, String> anotherMap = new HashMap<>();
Map<? extends A, String> aMap = anotherMap;
aMap.put(new MyObj(), ""); // Can't do this; but if you could...
MyOtherObj obj = anotherMap.keySet().iterator().next(); // ClassCastException!
Remember the acronym PECS (see this question for a lot more detail):
- Producer
extends
- Consumer
super
In other words, Map<? extends A, String>
can only be used to produce instances of A
, it can't consume/accept instances of A
.
For example, you can iterate the keys ("producing" the keys):
for (A a : aMap.keySet()) { ... }
The map can only "consume" a literal null
:
aMap.put(null, "");
because null
can be cast to any type without exception. But there's not much use in a map which only has a single key - you may as well just store the value directly.
The only way to do this type-safely is to put the instance of MyObj
into the map via a reference which you know accepts MyObj
instances:
Map<MyObj, String> safeMap = new HashMap<>();
safeMap.put(new MyObj(), "");
Map<? extends A, String> aMap = safeMap;
or
Map<A, String> safeMap = new HashMap<>();
safeMap.put(new MyObj(), "");
Map<? extends A, String> aMap = safeMap;
But you should consider not having the wildcard-typed map at all; Map<MyObj, String>
or Map<A, String>
is easier.