Copy map content to another hashtable

We can use putAll() method to copy map content to another hashtable in java.

Syntax:

hashtable.putAll(hashMap);

Example:

package com.w3spoint;
 
import java.util.HashMap;
import java.util.Hashtable;
 
public class Test {
  public static void main(String args[]){
	//Create Hashtable object.
	Hashtable<String, String> hashtable = new Hashtable<String, String>();
	//Add objects to the Hashtable.
	hashtable.put("1", "Jai");
	hashtable.put("2", "Mahesh");
	hashtable.put("3","Vivek");
        System.out.println(hashtable);
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("4", "Vishal");
        hashMap.put("5", "Hemant");
        hashtable.putAll(hashMap);
        System.out.println(hashtable);
  }
}

Output

{3=Vivek, 2=Mahesh, 1=Jai}
{5=Hemant, 4=Vishal, 3=Vivek, 2=Mahesh, 1=Jai}
Please follow and like us:
Content Protection by DMCA.com