This example shows how to replace a value for the key in Hashtable in Java. This example also shows how to replace the value for a key using the put method and replace method.
How to replace the value for a key in Hashtable in Java?
There are a couple ways using which we can replace a value mapped to the hashtable key.
1. Using the replace method
The Hashtable replace
method replaces the value mapped to the specified key with the specified new value.
1 |
public V replace(K key, V value) |
It replaces the old value with the given new value for the key in the hashtable object and returns an old value. If the key does not exist in the hashtable, it 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 |
import java.util.Hashtable; public class HashtableReplaceExample { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * To replace a value mapped to a key, use * the replace method */ /* * this will replace "One" with "Ten" and * return "One" for key 1 */ System.out.println( hashtable.replace(1, "Ten")); //this will return null as the key 4 does not exist System.out.println( hashtable.replace(4, "Forty")); System.out.println("Hashtable contains: " + hashtable); } } |
Output
1 2 3 |
One null Hashtable contains: {3=Three, 2=Two, 1=Ten} |
The above given replace
method replaces the value of the key if the key is mapped to any value in the hashtable. If you want to replace the value for a key if and only if it is mapped to a specific value, then use the overloaded replace
method.
1 |
public boolean replace(K key, V oldValue, V newValue) |
It replaces the old value with the given new value and returns true if the key exists in the hashtable and it is mapped to the given old value. If the key does not exist in the hashtable or if the key exists but not mapped to the given old value, it returns false.
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 |
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(2, "Two"); hashtable.put(3, "Three"); /* * this will replace "One" with "Ten" and * return true because key 1 * exists and it is mapped to value "One" */ System.out.println( hashtable.replace(1, "One", "Ten")); /* * this will return false as the key * 4 does not exist */ System.out.println( hashtable.replace(4, "Four", "Forty")); /* * this will also return false as the key * 3 exists but it is not mapped to value "Five" */ System.out.println( hashtable.replace(3, "Five", "Thirty")); System.out.println("Hashtable contains: " + hashtable); |
Output
1 2 3 4 |
true false false Hashtable contains: {3=Three, 2=Two, 1=Ten} |
2. Using the put method (not recommended)
The Hashtable put
method also replaces the value for the given key in the hashtable, if the key exists.
1 |
public V put(K key, V value) |
Using the put
method for the replacement purpose is not recommended because if the specified key does not exist in the hashtable, it creates a new mapping.
This example is a part of the Java Hashtable Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation