This Java HashMap replace example shows how to replace a value for the given key using the replace method of the HashMap class. The example also shows the difference between the put and replace method.
How to replace a value in Java HashMap?
There are several ways using which you can replace a value associated with the key in the HashMap object.
1. Using the put method
The put
method of the HashMap class replaces an old value with a new value for the given key if the key already exists in the map. It creates a new mapping if the key does not exist in the map.
1 |
public V put(K key, V value) |
The put
method returns an old value associated with the given key if it exists in the map
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 |
import java.util.HashMap; public class HashMapReplaceExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); System.out.println("HashMap contains: " + hMapNumbers); /* * To replace a value for a given key, use * the put method. * * It replaces an old value with the new one if * the key is mapped to some value in the map. If * the key does not exist, it creates a new mapping. */ //this will replace "Two" with "Twenty" for key 2 String oldValue = hMapNumbers.put(2, "Twenty"); System.out.println("HashMap contains: " + hMapNumbers); } } |
Output
1 2 |
HashMap contains: {1=One, 2=Two, 3=Three} HashMap contains: {1=One, 2=Twenty, 3=Three} |
Tip: If the key does not exist in the map, the put
method creates a new mapping for the specified key-value.
2. How to replace a value for given key using the replace method
The replace
method of the HashMap class replaces an old value with the new value for a given key, if and only if the mapping exists for the given key.
1 |
public V replace(K key, V value) |
The replace
method returns the old value mapped to the given key if the mapping exists for the given key. It returns null if the key is not mapped to any value or if the key was mapped to the null 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 |
import java.util.HashMap; public class HashMapReplaceExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); System.out.println("HashMap contains: " + hMapNumbers); /* * The replace method replaces an old value with * the new one if the key is mapped to some value * in the map. * * If the key does not exist in the map, it returns null. */ //this will replace "Two" with "Twenty" for key 2 and returns "Two" System.out.println( hMapNumbers.replace(2, "Twenty") ); System.out.println("HashMap contains: " + hMapNumbers); //this will return null as key 4 does not exist in the map System.out.println( hMapNumbers.replace(4, "Four") ); System.out.println("HashMap contains: " + hMapNumbers); } } |
Output
1 2 3 4 5 |
HashMap contains: {1=One, 2=Two, 3=Three} Two HashMap contains: {1=One, 2=Twenty, 3=Three} null HashMap contains: {1=One, 2=Twenty, 3=Three} |
3. How to replace a value for a given key only if it is mapped to a specified value
The replace
method given above replaces a value for the given key if the key is mapped to any value in the map. What if you want to replace a value for the key if and only if it is mapped to a specific value in the map. In that case, you can use the overloaded replace
method.
1 |
public boolean replace(K key, V oldValue, V newValue) |
This replace
method returns true if and only if the key is mapped to the oldValue in the map and replaced with the specified newValue. It returns false if the mapping for the key does not exist in the map or if the key is mapped to some other 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 40 |
import java.util.HashMap; public class HashMapReplaceExample { public static void main(String[] args) { HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); System.out.println("HashMap contains: " + hMapNumbers); /* * This will replace "Two" with "Twenty" because * the key 2 is currently mapped to value "Two" and * returns true */ System.out.println( hMapNumbers.replace(2, "Two", "Twenty") ); System.out.println("HashMap contains: " + hMapNumbers); /* * This will not replace value for key 3 as the * key 3 is not mapped to a value "Thirty" and * hence returns false */ System.out.println( hMapNumbers.replace(3, "Thirty", "Forty") ); System.out.println("HashMap contains: " + hMapNumbers); /* * This will not do any replacement as the key * 4 does not exist in the map and so returns false */ System.out.println( hMapNumbers.replace(4, "Four", "Forty") ); System.out.println("HashMap contains: " + hMapNumbers); } } |
Output
1 2 3 4 5 6 7 |
HashMap contains: {1=One, 2=Two, 3=Three} true HashMap contains: {1=One, 2=Twenty, 3=Three} false HashMap contains: {1=One, 2=Twenty, 3=Three} false HashMap contains: {1=One, 2=Twenty, 3=Three} |
What is the difference between the put and replace methods?
The put
method and replace
method behaves the same if the key exists in the map. Both of them replace an old value with the new one and return the old value for the given key. But if the key does exist in the map, the put
method creates a new mapping while the replace
method returns null.
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.