Return Value of put() in HashMap
package com.payu.Payu;
import java.util.*;
public class HashMap_Example {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
// Mapping string values to int keys
hashmap.put(10, "HashMap");
hashmap.put(15, "4");
hashmap.put(25, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hashmap);
// Inserting existing key along with new value
// return type of put is type of values i.e. String and containing the old value
String returned_value = hashmap.put(10, "abc");
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Inserting new key along with new value
// return type of put is type of values i.e. String ; since it is new key ,return value will be null
returned_value = hashmap.put(20, "abc");
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Displayin the new map
System.out.println("New map is: " + hashmap);
}
}
Output :-
Initial Mappings are: {25=You, 10=HashMap, 15=4}
Returned value is: HashMap
Returned value is: null
New map is: {20=abc, 25=You, 10=abc, 15=4}
The method put has a return type same with the value:
@Override
public V put(K key, V value) {
return putImpl(key, value);
}
The method associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
It returns the previous value associated with key, or null if there was no mapping for key.So, your points are right.
For more details please visit here