This example shows how to add key-value mappings to HashMap in Java using the put method. The example also shows how to add key-value mapping only if the key does not exist using the putIfAbsent method.
How to add key-value mappings to HashMap using the put method?
The put
method of the HashMap class adds specified key and value mapping to the map object.
1 |
public V put(K key, V value) |
If the specified key does not exist in the map, the put
method maps the key with the given value and returns null. If the key is already mapped to any value in the map object, the old value is replaced with the specified new value and the old value is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.HashMap; public class HashMapPutExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); /* * This will map key 1 with value "One" in * the HashMap object. It returns null because * the key 1 does not exist in the map. */ System.out.println( hMapNumbers.put(1, "One") ); /* * This will map key 2 with value "Two" in * the HashMap object. It returns null because * the key 2 does not exist in the map. */ System.out.println( hMapNumbers.put(2, "Two") ); /* * This will map key 1 with value "Eleven" in * the HashMap object. It returns "One" because * the key 1 was previously mapped to value "One" in the map. */ System.out.println( hMapNumbers.put(1, "Eleven") ); System.out.println("HashMap contains: " + hMapNumbers); } } |
Output
1 2 3 4 |
null null One HashMap contains: {1=Eleven, 2=Two} |
How to add new mapping only if the key does not exist in the HashMap?
As you can see from the output if the key already exists in the map, the put
method replaces an old value with the specified new value for a given key.
If want to add new mapping if the key does not exist but you do not want to replace the old value if the key already exists in the map, you can use the putIfAbsent
method of the HashMap class.
1 |
public V putIfAbsent(K key, V value) |
The putIfAbsent
method adds the specified key-value mapping to the map object if the key does not exist in the map or if the key is mapped to a null value. If the key is mapped to any non-null value in the map, the putIfAbsent
method does nothing and returns the existing value mapped to the given key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); hMapNumbers.put(1, "One"); hMapNumbers.put(2, null); hMapNumbers.put(3, "Three"); /* * This will not do anything as the key 1 already * exists in the map. It will return "One", the * value currently associated with the key 1 */ System.out.println( hMapNumbers.putIfAbsent(1, "Eleven") ); /* * This will map value "Two" with key 2, as the key 2 * already exists in the map but currently associated with * the null value. It returns null, the value currently * associated with the key 2. */ System.out.println( hMapNumbers.putIfAbsent(2, "Two") ); /* * This will add mapping 4=>Four to the map as * the key 4 is not mapped to any value in the map * and returns null. */ System.out.println( hMapNumbers.putIfAbsent(4, "Four") ); System.out.println("HashMap contains: " + hMapNumbers); |
Output
1 2 3 4 |
One null null HashMap contains: {1=One, 2=Two, 3=Three, 4=Four} |
Difference between put and putIfAbsent methods
If the key does not exist in the map, both put
and putIfAbsent
methods create a new mapping in the HashMap object. But if the key is already mapped to any non-null value in the map, the put
method replaces an old value mapped to the specified key with the new value in the map while the putIfAbsent
method does nothing.
Please also see Java HashMap get example and Java HashMap replace example to know more.
This tutorial is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.