public interface A {
int getA();
}
public class MyObj implements A {
public int getA(){
return 1;
}
}
If have a Map : Map<? extends A, String> aMap = new HashMap<>();
How can I add an MyObj to this aMap ? Or how should be the class MyObj so that it can work in this map
How can i add an
MyObjto thisaMap?
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):
extendssuperIn 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With