How to Copy Map Content to Another Hashtable in Java


Here this example will show how to copy a map content to another hashtable. If you want to copy the contents of a map into another hashtable, you may use the putAll(Map m) method of the Hashtable object, as shown below.

Source Code

package com.beginner.examples;

import java.util.HashMap;
import java.util.Hashtable;

public class CopyMapDemo {

	public static void main(String[] args) {
		
		HashMap map = new HashMap();
		
		map.put("1","A");
		map.put("2","B");
		map.put("3","C");
		map.put("4","D");
		map.put("5","E");
		
		Hashtable table = new Hashtable();
		
		table.putAll(map);
		
		System.out.println(table);
		
	}
}

Output:

{5=E, 4=D, 3=C, 2=B, 1=A}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments