This example shows how to use Java Hashtable put, putIfAbsent, and putAll method to add key-value mappings or entries in the hashtable object.
How to add new key-value mappings to Hashtable using the put method?
The Hashtable put
method maps the specified key to the specified value in the hashtable object.
1 |
public V put(K key, V value) |
If the specified key already exists in the hash table, the put
method replaces the old value with the specified new value and returns the old value. if the key does not exist, it creates a new mapping and 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 HashtableAddMappingsExample { public static void main(String[] args) { Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); /* * Use the put method to add key-value mappings * to the hashtable */ /* * this will add 1=>"One" mapping to the hashtable * and return null as the key 1 does not exist */ System.out.println( htNumbers.put(1, "One") ); /* * this will replace the existing value "One" with * new value "Ten" for key 1 and return the old value * i.e. "One" */ System.out.println( htNumbers.put(1, "Ten") ); System.out.println("Hashtable contains: " + htNumbers); } } |
Output
1 2 3 |
null One Hashtable contains: {1=Ten} |
How to add new key-value mappings to Hashtable using the putIfAbsent method?
As we can see from the previous output, the put
method replaces an old value with the specified new value if the key already exists in the hashtable object.
If you want to add a new mapping if the key does not exist but do not want to replace the value if the key already exists, then you can use the Hashtable putIfAbsent
method.
1 |
public V putIfAbsent(K key, V value) |
The putIfAbsent
method maps the specified key to the specified value if the key does not already exist in the hashtable and returns null. if the key already exists, it simply returns the existing value associated with the given key and does not do any replacement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Hashtable<Integer, String> htNumbers = new Hashtable<Integer, String>(); /* * this will add 1=>"One" mapping to the hashtable * and return null as the key 1 does not exist */ System.out.println( htNumbers.putIfAbsent(1, "One") ); /* * this will not replace "One" with "Ten" and return * "One" i.e. value mapped to key 1 */ System.out.println( htNumbers.putIfAbsent(1, "Ten") ); System.out.println("Hashtable contains: " + htNumbers); |
Output
1 2 3 |
null One Hashtable contains: {1=One} |
As we can see from the output, the putIfAbsent
method does not replace the value if the key is already present in the hashtable.
How to copy mappings from any map object to Hashtable using the putAll method?
The Hashtable putAll
method copies all the mappings of the specified map object to this hashtable object.
1 |
public void putAll(Map<? extends K,? extends V> map) |
The mappings contained in the specified map will replace the mappings in the hashtable for the matching keys.
The below given example shows how to copy all mappings from the HashMap class object to the hashtable object using the putAll
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
HashMap<Integer, String> hashmap = new HashMap<Integer, String>(); hashmap.put(1, "One"); hashmap.put(2, "Two"); hashmap.put(3, "Three"); Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashmap.put(1, "Ten"); /* * To copy all the mappings from any map * to this hashtable object, use the * addAll method */ hashtable.putAll(hashmap); System.out.println("Hashtable contains: " + hashtable); |
Output
1 |
Hashtable contains: {3=One, 2=One, 1=Ten} |
As we can see from the output, the value “Ten” for key 1 in the hashtable was replaced by the value “One” in the hashmap object.
Important Note:
The Hashtable class does not allow null keys or values. The put, putIfAbsent, and putAll methods throw NullPointerException if any key or value is specified as null.
This example is a part of the Hashtable in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation