So I have a Map that has some values in it being passed into a method:
public String doThis(Map<String, ?> context){
.....
}
And I’m trying to insert an addition attribute to this Map
String abc="123";
context.put("newAttr",abc);
But I am getting this error:
The method put(String, capture#8-of ?) in the type Map is not applicable for the arguments (String, String)
Is there anyway to perform this put without “cloning” the Map?
If you want to put values of type X into a generic
Mapyou need to declare theMapasMap<String, ? super X>. In your example X isString, so:Map<String, ? super X>means: a map with keys of typeStringand values of a type which is X or a super-type of X. All such maps are ready to acceptStringinstances as keys and X instances as values.