I have a method that returns a map defined as:
public Map<String, ?> getData();
The actual implementation of this method is not clear to me, but, when I try to do:
obj.getData().put("key","value")
I get following compile time error message:
The method put(String, capture#9-of ?)
in the type Map
is not applicable for the arguments
(String, String)
What is the problem? Is String not of type anything?
Thanks in advance.
The wildcard means “the value type parameter could be anything” – it doesn’t mean “you can use this as if it were anything you want it to be”. In other words, a
Map<String, UUID>is valid as aMap<String, ?>– but you wouldn’t want to be able to put a String value into it.If you want a map which can definitely accept string values, you want: