0

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)

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

HashMap<String, ? super String> can be replaced with HashMap<String, String> with same result as String is final and cant be extended
@lordzden so if it works, can you accept answer as working one also for others? Thanks.
0

The problem is you are using the wildcard "?". This is basically an unknown type. You should probably use String type instead, or Object if you need a more generic type. Like so:

ArrayList<HashMap<String, String>>

You can read more about it here: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html and here: http://docs.oracle.com/javase/tutorial/extra/generics/morefun.html

Hope it helps.

Good luck

Comments

0

this is code;

ArrayList<HashMap<String,String>> hashArray=new 
hashArray.get(index).remove("key");
hashArray.get(index).put("key","value");

ok

Comments

-1

You can try toString implemented in the type of "quantity".

Otherwise simply, quantity + ""

1 Comment

The problem is that String is not of type ?. It has nothing to do with quantity not being a String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.