Home » Java » java – What does map.put return?-Exceptionshub

java – What does map.put return?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I tried to get the return type of put method of the map interface. When I print for the first time it is printing null and after updating the key I get the previous value.
So, can anyone tell me what is the return type of the put method in the map interface?

Map<Integer, String> map = new HashMap<Integer, String>();
System.out.println(map.put(1, "ABC"));
System.out.println(map.put(1, "XYZ"));

Output:
null
ABC
How to&Answers:

According to documentation, it’s a generic function that returns the same datatype as the map value. So, in your case it should return String.

Answer:

As per java docs:

The put returns the previous value associated with key, or null if
there was no mapping for key. (A null return can also indicate that
the map previously associated null with the key if the implementation
supports null values.)

In your case when you did map.put(1, "ABC") nothing was associated with key 1 so it returns null but when you use put(1, "XYZ") then there was already an entry exists against the key 1 so it returns “ABC”

Answer:

you can read in the doc: here

V put(K key, V value)

Returns:
the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map
previously associated null with key, if the implementation supports
null values.)

so, since your map is declared as

Map<Integer, String> map

the Value V is the String type, and therefore calling put will return string.

but what is the value that I get? may you be asking to yourself….

then take a deep look in the source code…

HashMap’s put method is actually invoking the final method putVal

and is implemented like:

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

and in the logic, the method returns the previous value found in the map for the given key…

that means the first time you do

put(1,"abc");
it returns null, because there was nothing in the map.
if you do again
put(1,"xyz"); then the method is returning “abc“, because is the last value found in the map for the key = 1

Answer:

This method returns returns previous value associated with the key if present, else return null. So return type in above case is String.