This example shows how to add key-value mappings to the TreeMap using the put method in Java. This example also shows how to add entries to TreeMap only if the key does not exist using the putIfAbsent method.
How to add key-value mappings to TreeMap using the put method in Java?
The put
method of the TreeMap class adds specified key-value mappings to the TreeMap object.
1 |
public V put(K key, V value) |
The put
method adds the specified key-value pair to the TreeMap and returns null if the specified key does not exist in the TreeMap object. If the key exists in the TreeMap, the put
method replaces old value associated with the specified key with the new value and returns the old value.
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 34 35 36 37 38 39 |
import java.util.TreeMap; public class TreeMapPutExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); /* * To add key-value mappings to the TreeMap, use * the put method */ /* * If the specified key does not exist, the put * method adds new mapping and returns null */ //this will add 1=>"One" to the map and return null System.out.println( tmapColors.put(1, "One") ); //this will add 2=>"Two" to the map and return null System.out.println( tmapColors.put(2, "Two") ); System.out.println("TreeMap contains: " + tmapColors); /* * If the specified key exists, the put * method replaces old value with new value and * returns old value */ //this will replace "One" with "Eleven" and return "One" System.out.println( tmapColors.put(1, "Eleven") ); System.out.println("TreeMap contains: " + tmapColors); } } |
Output
1 2 3 4 5 |
null null TreeMap contains: {1=One, 2=Two} One TreeMap contains: {1=Eleven, 2=Two} |
How to prevent replacement if the key already exists in the TreeMap?
As you can see from the output, the put
method replaces an old value with the new value if the key already exists in the TreeMap. What if you do not want the replacement to happen and only want to add new mapping if the key does not exist in the TreeMap?
Well, in that case, you can use the putIfAbsent
method instead of the put
method.
1 |
public V putIfAbsent(K key, V value) |
The putIfAbsent
method adds specified key-value mapping to the TreeMap if the key does not exist and returns null. If the key exists, the putIfAbsent
method returns the existing value mapped to the given key.
1 2 3 4 5 6 7 8 9 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); //this will add 1="One" and return null System.out.println( tmapColors.putIfAbsent(1, "One") ); //this will just return "One" as the key 1 exists in the map System.out.println( tmapColors.putIfAbsent(1, "Eleven") ); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 3 |
null One TreeMap contains: {1=One} |
Difference between put and putIfAbsent methods
If the key does not exist in the TreeMap, both put
and putIfAbsent
methods behave the same. They both add key-value mapping to the map and return null.
However, when the specified key exists in the TreeMap, the put
method replaces an old value with the new value while the putIfAbsent
method does not replace.
Please also visit how to get value for the given key from TreeMap example.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap