10

I wish to get a value from a HasMap but some time the value doesn't exist so I have done this:

int var = 0;
if (hashMapHouse.containsKey("home1") {
    var = hashMapHouse.get("houme1");
}
if(var==0) //do something
else //do something else

My question is: is it possible to have one call of hashMap in order to get the value and to test if the value exist or no?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
snorlax
  • 157
  • 2
  • 3
  • 10

4 Answers4

27

In Java 8 you can use the getOrDefault method:

int var = hashMapHouse.getOrDefault("home1", 0);

It's the cleanest solution, in a single line you can get either the value for the key if it exists, or a predefined default value to indicate that it doesn't exist - 0 in this case.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

Sure

Integer houme = hashMapHouse.get("houme1");

if (null == houme ) {
    // not exists 
} else {
    // exists 
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
2

From the docs:

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

If you are not using null as value then you can use the get() method once and do it like this:

Integer var = hashMap.get(key);
if (var != null) {
    // value exists
}
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • Who *(provide link, please)* is "not recommending" the method, and what do they recommend instead? Given that most recommendations are to not store `null` in a Map (especially since some maps don't even allow it), why would it not be recommended to rely on `get()` returning `null` as meaning "not found", since it outperforms having to call `containsKey()` first? – Andreas Nov 19 '16 at 15:01
  • @Andreas We can explicitly keep `null` against a `key` in a `Map`. So if that's the case for OP, then it would not be a good idea to use just `get()` to determine if `key-value` exists or not. Correct me if I'm wrong – denvercoder9 Nov 19 '16 at 15:05
  • 1
    *If* the Map *will* contain null values, calling `get()` will not be able to distinguish "missing" from "there with null". If you need to know the difference, then yes, you'll need to call `containsKey()`. Most of the time, a Map will not contain null values *(especially guaranteed when using Maps that won't allow it)*, so there is no ambiguity in those cases. --- But that's beside the point. You said *"this is not **the** recommended method"*, and I was asking you **who** made that recommendation, because I haven't seen/heard it before. – Andreas Nov 19 '16 at 15:14
  • @Andreas Okay, I agree I gave my opinion without backing it up. I'll remove it – denvercoder9 Nov 19 '16 at 15:19
  • It's ok to give your opinion, but you should phrase it so people will know it's *your* opinion. You had phrased it as being a generally accepted recommendation. Instead, you could have said "But I wouldn't recommend it because ...", or something like that. – Andreas Nov 19 '16 at 15:22
1

It's possible to fetch the value while checking the condition, to avoid accessing the HashMap twice.

if ((temp = hashMapHouse.get("home1") != null) {
    var = temp;
}

temp variable are set in the same time we verify our condition.

zyydoosh
  • 387
  • 2
  • 14