1

I have a HashMap with String key and String value. I want to get an item from list, I tried to give key and wanted to get value but it gives an error. The following example how can I get "both" value with give the key "blazer"?

 HashMap<String,String> upper = new HashMap<>();
 upper.put("shoulder","both");
 upper.put("blazer","both");

 if(upper.get(upper.get("blazer"))) {} //gives an "incompatible types" error. 
 //Error: Required: boolean Found: java.lang.String
4
  • The code shown compiles fine. Commented May 29, 2018 at 16:47
  • 1
    You'll get null in this case since upper.get(upper.get("blazer")); is the same as upper.get("both"); and since you don't have an entry with a key value of both you'll get null. It's not clear what you were going for. Commented May 29, 2018 at 16:52
  • @Barns so sorry, I edited my question and solve it. Commented May 29, 2018 at 18:12
  • That is better. Now, (even though you did not post the stack trace) we can understand why you are getting the error. Removing downvote. Commented May 29, 2018 at 18:14

2 Answers 2

2

Understand that upper.get(key) will not return a boolean value. You have defined your HashMap as follows:

HashMap<String,String> upper = new HashMap<>();

This means that both the key and value will be of type String. Thus, providing a valid key the the get() method will return a String:

String myValue = upper.get("blazer");

If you wish to check if a key is available before you attempt to read the value you can use the method containsKey() with will return a boolean value indicating whether the HashMap contains an entry with the given key:

if(upper.containsKey("blazer")){
    String myValue = upper.get("blazer");
    Log.e(TAG, "Yes blazer is available : " + myValue);
} 
else{
    Log.e(TAG, "No blazer is available!");
}

You can also iterate through the available keys like this:

Set<String> set = map.keySet();
for(String s : set){
    Log.e(TAG, "Map key = " + s + " value = " + map.get(s));
}
Sign up to request clarification or add additional context in comments.

Comments

1

They way you have it there upper.get(upper.get("blazer")); would just return null.

You're passing in upper.get("blazer") (which would return "both") to your outer upper.get. Since you have no "both" key stored in your map, it returns null.

Should be:

upper.get("blazer");

5 Comments

Why should it be that? It's unclear what OP is trying to do.
To get a value from a hashmap, you need to give it the key map.get(key). In this case the key is "blazer"
OP is already giving a key of the correct type, for both calls to upper.get.
True, what OP is doing is the same as upper.get("both"); and since there is no key "both" it doesn't work. Not sure what's up with the incompatible types error though, maybe we're missing something
@Sacha sorry, that's my fault. I copied and pasted code block, that line was in the if case. if(upper.get(upper.get("blazer"))) so it gave me incompatible types error. I realized my fault when you answered the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.