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
Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70
Selin Gök
  • 331
  • 1
  • 5
  • 20
  • [The code shown compiles fine](https://ideone.com/JqWD6w). – Andy Turner May 29 '18 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. – zlb323 May 29 '18 at 16:52
  • @Barns so sorry, I edited my question and solve it. – Selin Gök May 29 '18 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. – Barns May 29 '18 at 18:14

2 Answers2

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));
}
Barns
  • 4,850
  • 3
  • 17
  • 31
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");
toltman
  • 467
  • 2
  • 6
Sacha
  • 322
  • 1
  • 8
  • 1
    Why should it be that? It's unclear what OP is trying to do. – Andy Turner May 29 '18 at 16:47
  • To get a value from a hashmap, you need to give it the key `map.get(key)`. In this case the key is `"blazer"` – Sacha May 29 '18 at 16:50
  • OP is already giving a key of the correct type, for both calls to `upper.get`. – Andy Turner May 29 '18 at 16:51
  • 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 May 29 '18 at 16:56
  • @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. – Selin Gök May 29 '18 at 18:04