rahstame rahstame - 1 year ago 38
Android Question

Update ArrayList<HashMap<String, ?>>

How do I update

ArrayList<HashMap<String, ?>>
?
quantity is
String


values.get(position).put(KEY_POSITION, quantity);


Error:


The method put(String, capture#11-of ?) in the type
HashMap is not applicable for the arguments
(String, String)

Answer Source

So problem is related into logic of wildcards. Wildcard means "value type parameter can be anything" so HashMap<String, ?> is valid but here you're not able to put a String value into it.

Note: HashMap<String, ?> is same as HashMap<String, ? extends Object> so ? can be any type of class but String cannot be any type of class. This is reason of your error.

You need to make small change:

HashMap<String, ? super String>

Or as suggested @user902383 directly change1 (with same result) it into:

HashMap<String, String>

Now it will accept Strings.

1 String class is final (you can't extend from it) - for this reason it's better to use HashMap<String, String> First approach with HashMap<String, ? super String> is "useless" since you're not able to extend from String class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download