oldmansaur
oldmansaur

Reputation: 163

Check if key is in map - one way works and the other doesn't

I am trying to check whether a key is in a map(Java) and am a bit confused about why one of my solutions works and the other doesn't.

In particular: when I compare myMap.get(s) directly to null it works but when I first save myMap.get(s) to a variable (number) and then compare to null it throws a The operator != is undefined for the argument type(s) int, null error.

Code that works:

import java.util.*;
import java.io.*;

class test1{
    public static void main(String []argh){
        // Create a map
        Map<String, Integer> myMap; 
        myMap = new HashMap<String, Integer>();
        // Make an entry in the map
        String key = "hello";
        int value = 5;
        myMap.put(key, value);


        String s = "hi";

        if (myMap.get(s) != null)
        {
            int number = myMap.get(s);
            System.out.printf("%s\n", number);
        }else
        {
            System.out.println("Not in dict");
        }
    }
}

Code that does not work:

import java.util.*;
import java.io.*;

class test2{
    public static void main(String []argh){
        // Create a map
        Map<String, Integer> myMap; 
        myMap = new HashMap<String, Integer>();
        // Make an entry in the map
        String key = "hello";
        int value = 5;
        myMap.put(key, value);


        String s = "hi";
        int number = myMap.get(s);
        if (number != null)
        {
            System.out.printf("%s\n", number);
        }else
        {
            System.out.println("Not in dict");
        }
    }
}

I would like to know how I am supposed to understand this, because to me myMap.get(s) is also just an int?

Thanks for reading.

Upvotes: 0

Views: 74

Answers (4)

Jai
Jai

Reputation: 8363

int number = myMap.get(s); returns an Integer and it can be null. Therefore when it is null, the primitive int cannot hold that value and will cause an exception. Either use Integer to store the value, or use contains() to check before accessing.

Upvotes: 0

Ildelian
Ildelian

Reputation: 1318

In the second case, you are storing the value on a "int" varible, that is a primivitive and never can be null. Change it to "Integer" and it works.

Upvotes: 0

QBrute
QBrute

Reputation: 4544

number is of primitive type int and therefore cannot be null.
If you instead write Integer number = myMap.get(s);, your null-check would work.

But to check if a key is present, you should use containsKey(...) instead.

Upvotes: 0

Eran
Eran

Reputation: 393936

An int can never be null, but an Integer can.

myMap.get(s) returns an Integer, which can be null.

int number = myMap.get(s);

would throw a NullPointerException if myMap.get(s) returns null.

If you want to safely assign the value to a variable, use an Integer variable:

Integer number = myMap.get(s);
if (number != null) {
    System.out.printf("%s\n", number);
} else {
    System.out.println("Not in dict");
}

Upvotes: 2

Related Questions