Open In App
Related Articles

Java Program to Copy the Map Content to Another Hashtable

Improve Article
Improve
Save Article
Save
Like Article
Like

HashMap and Hashtable are used to store data in key and value form using a hashing technique to store unique keys. To copy Map content to another Hashtable in Java putAll() method is used.

putAll() method: 

The method copies all the mappings from the specified hashmap to the hashtable. These mappings replace any mappings that this hash table had for any of the keys currently in the specified hashmap.

Syntax:

hashtable.putAll(hashmap)

Parameters: The method takes one parameter hashmap that refers to the existing map we want to copy from

Return Value: The method does not return any values.

Exception: The method throws NullPointerException if the map we want to copy from is null.

Steps in Copying HashMap elements to a Hashtable

  1. Create a new HashMap with some elements
  2. Put mappings into HashMap
  3. Create a new Hashtable.
  4. Use putAll() method to copy elements from HashMap to Hashtable

Example 1:

Java




// Java code to copy Map content to another Hashtable
  
import java.util.HashMap;
import java.util.Hashtable;
public class NewExample {
    public static void main(String a[])
    {
        // Create hashmap and insert elements
        HashMap<String, String> hashmap
            = new HashMap<String, String>();
        
          // Add mappings
        hashmap.put("key_1", "GeeksForGeeks");
        hashmap.put("key_2", "2020");
        
        // Create hashtable
        Hashtable<String, String> hashtable
            = new Hashtable<String, String>();
        
        // Use putAll to copy Map elements to hashtable.
        hashtable.putAll(hashmap);
        
        // Print hashtable elements
        System.out.println("Elements in hashtable: "
                           + hashtable);
    }
}


Output

Elements in hashtable: {key_2=2020, key_1=GeeksForGeeks}

Example 2:

Java




// Java code to copy Map content to another Hashtable
  
import java.util.Hashtable;
import java.util.HashMap;
  
public class HashmapExample {
    public static void main(String[] args)
    {
        // Create HashMap and insert elements
        HashMap<String, String> hashmap
            = new HashMap<String, String>();
        
          // Add mappings
        hashmap.put("key_1", "GeeksForGeeks");
        hashmap.put("key_2", "2020");
  
        // Create Hashtable and insert elements
        Hashtable<String, String> hashtable
            = new Hashtable<String, String>();
        hashtable.put("key_1", "GFG");
        hashtable.put("key_3", "Java");
        hashtable.put("key_4", "Programming");
  
        System.out.println("Elements in HashMap : "
                           + hashmap);
        System.out.println(
            "Initial Elements in Hashtable : " + hashtable);
  
        // Use putAll() to copy Map elements to Hashtable
        // It replaces existing mapping of keys
        hashtable.putAll(hashmap);
  
        System.out.println(
            "After copying map elements in hashtable:");
        System.out.println("Elements in Hashtable : "
                           + hashtable);
    }
}


Output

Elements in HashMap : {key_2=2020, key_1=GeeksForGeeks}
Initial Elements in Hashtable : {key_4=Programming, key_3=Java, key_1=GFG}
After copying map elements in hashtable:
Elements in Hashtable : {key_4=Programming, key_3=Java, key_2=2020, key_1=GeeksForGeeks}

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Dec, 2020
Like Article
Save Article
Similar Reads
Related Tutorials