This example shows how to replace a value for the given key in the TreeMap in Java. The example also shows how to replace a value in TreeMap using the put and replace methods.
How to replace a value for key in TreeMap in Java?
There are a couple of ways using which we can replace a value mapped to the given key in the TreeMap object.
1. Using the put method
The put
method of the TreeMap class replaces an old value with the specified new value for the given key.
1 |
public V put(K key, V value) |
The put
method returns the old value mapped to the specified key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.TreeMap; public class TreeMapReplaceValueExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * The put method replaces an old value * with the specified new value for the * already existing keys of the TreeMap */ //this will replace "Red" with "Black" for key 1 and return "Red" System.out.println( tmapColors.put(1, "Black") ); System.out.println("TreeMap contains: " + tmapColors); } } |
Output
1 2 |
Red TreeMap contains: {1=Black, 2=Green, 3=Blue} |
Important Note: If the key does not exist in the TreeMap, the put
method creates a new mapping in the map object. Do not use this method to replace value if you do not want to add a new mapping to the TreeMap if the specified key does not exist.
2. Using the replace method
The replace
method of the TreeMap class replaces an old value with the specified new value for the given key in the map.
1 |
public V replace(K key, V value) |
The replace
method also returns the old value mapped to the given key if the key exists in the TreeMap. If the key does not exist in the map or the key was previously mapped to a null value, the replace
method returns null.
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.TreeMap; public class TreeMapReplaceValueExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, null); tmapColors.put(3, "Blue"); /* * This will replace value "Red" with "White" and * return "Red" for key 1 */ System.out.println( tmapColors.replace(1, "White") ); /* * This will replace null with "Brown" and * return null for key 2 */ System.out.println( tmapColors.replace(2, "Brown") ); /* * This will also return null as the key 4 does not exist */ System.out.println( tmapColors.replace(4, "Yellow") ); System.out.println("TreeMap contains: " + tmapColors); } } |
Output
1 2 3 4 |
Red null null TreeMap contains: {1=White, 2=Brown, 3=Blue} |
How to replace a value for the key only if it is mapped to a specific value?
The above given replace
method replaces the value for the key if the key is mapped to any value in the map. What if you want to replace a value only if the key is mapped to a specific value? In that case, you can use the overloaded replace
method that also accepts the old value parameter.
1 |
public boolean replace(K key, V oldValue, V newValue) |
This replace
method replaces the value of the key with newValue only if the key is mapped to the specified oldValue. This replace
method returns true if the value is replaced for the key, false otherwise.
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 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * This will replace "Red" with "White" and return true * because key 1 exists and it is currently mapped to "Red" */ System.out.println( tmapColors.replace(1, "Red", "White") ); /* * This will return false because key 3 is not mapped to * the value "Brown" */ System.out.println( tmapColors.replace(3, "Brown", "Purple") ); /* * This will return false as the key 4 does not exist */ System.out.println( tmapColors.replace(4, "Blue", "Black") ); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 3 4 |
true false false TreeMap contains: {1=White, 2=Green, 3=Blue} |
This example is a part of the TreeMap In Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap