JavaNewbie_M107
JavaNewbie_M107

Reputation: 2037

Strange results with null in Map

I ran the following code

public class MapTest {    
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put(null, null);
        System.out.println(map.get(null));
        System.out.println(map.containsKey(null));
        System.out.println(map.containsValue(null));
    }
}

And it gave this output

null
true
true

However, when I remove the line map.put(null, null), map.get(null) still returns null, but map.containsKey(null) & map.containsValue(null) return false. Now, if the value null is not associated with any key, how come it is possible that map.get(null) still returns null?

Upvotes: 0

Views: 1211

Answers (5)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

There is no Strange behavior here. Mapis a interface in java collection framework and HashMap is implementaion.

Simply map is a data structure allows to store key,value pair.

in here you are putting null as key and null as value. your HashMap<String,Integer> accept these value since String and Integers default value is null.

Then your are calling

map.get(null) // argument is a key and return value assign to that key. So you are getting null.

map.containsKey(null)// this method return true if particular key is there. In this case null is your key since return true map.containsKey(null) // this method return true if there is null as value in your may. This case this is also true.

And the next time your are getting null since map is empty and Integers default value is null. and obviously other two method return false since map is empty.

Upvotes: 0

ritesh
ritesh

Reputation: 917

map.get(null) returns null because:

Java Hash Map get Method

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

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.

This is possible:

map.put(null, null);

because:

Java Hash Map

This implementation provides all of the optional map operations, and permits null values and the null key.

Upvotes: 0

Jops
Jops

Reputation: 22715

Here's an except of the get() source code of HashMap:

public V get(Object key) {
   if (key == null)
     return getForNullKey();
   ...
}

If the key passed is null, it gets that key and returns the value associated to it, else it returns null.

 private V getForNullKey() {
     for (Entry<K,V> e = table[0]; e != null; e = e.next) {
         if (e.key == null)
            return e.value;
     }
     return null;
 }

Upvotes: 0

Devolus
Devolus

Reputation: 22084

If the map doesn't find the value in the map, then get(object) still returns null.

So for any arbitrary values you will get null as well.

Upvotes: 0

andersschuller
andersschuller

Reputation: 13907

The Javadoc for the Map interface states that get should return null for any key that does not have a mapping:

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Note however that some Map implementations may not allow null keys, in which case map.get(null) will throw a NullPointerException. An example of this is the ConcurrentHashMap.

Upvotes: 2

Related Questions