Java Hashtable putAll(Map t) Example

The put(K key, V value) method of java.util.Hashtable class associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced whereas putAll(Map<? extends K,? extends V> t) method copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Signature

public void putAll(Map<? extends K,? extends V> t)

This method copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map. Throws java.lang.NullPointerException if the specified map is null

Example

The following example shows the usage of java.util.Hashtable.putAll(t) method.

import java.util.Hashtable;
import java.util.Map;

public class PutAllMethodHashtableExample 
{
    public static void main(String args[])
    {
        // create an empty Hashtable
        Hashtable<Integer,String> hashtable1 = new Hashtable<Integer,String>();
        Hashtable<Integer,String> hashtable2 = new Hashtable<Integer,String>();
        
        // use put() method to put elements to the Hashtable1
        hashtable1.put(1,"Element1");
        hashtable1.put(2,"Element2");
        hashtable1.put(3,"Element3");
        hashtable1.put(4,"Element4");
        hashtable1.put(5,"Element5");
        
        System.out.println("**Elements of hashtable1 before putAll()**");
        
        //Print the elements of hashtable1
        for (Map.Entry<Integer,String> entry : hashtable1.entrySet())
        {
            System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue());
        }
        
        // use put() method to put elements to the Hashtable2
        hashtable2.put(10,"Element10");
        hashtable2.put(11,"Element11");
        hashtable2.put(12,"Element12");
        hashtable2.put(13,"Element13");
        hashtable2.put(14,"Element14");
        
        //Put all the elements of hashtable2 to hashtable1
        hashtable1.putAll(hashtable2);
        
        System.out.println("**Elements of hashtable1 after putAll()**");
        //Print the elements of hashtable1
        for (Map.Entry<Integer,String> entry : hashtable1.entrySet())
        {
            System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue());
        }
    }
}

Output

**Elements of hashtable1 before putAll()**
Key : 5 Value : Element5
Key : 4 Value : Element4
Key : 3 Value : Element3
Key : 2 Value : Element2
Key : 1 Value : Element1
**Elements of hashtable1 after putAll()**
Key : 14 Value : Element14
Key : 13 Value : Element13
Key : 12 Value : Element12
Key : 11 Value : Element11
Key : 10 Value : Element10
Key : 5 Value : Element5
Key : 4 Value : Element4
Key : 3 Value : Element3
Key : 2 Value : Element2
Key : 1 Value : Element1

Leave a Reply

Your email address will not be published. Required fields are marked *