Example of the putAll() method of Hashtable in Java
This tutorial shows how to copy all key-value pairs from one Hashtable to another in Java using the java.util.Hashtable.putAll().public void putAll(Map extends K,? extends V> t): This method is used to copy all elements from the Map specified to Hashtable. It throws an exception nullPointerException if the Map object is null.
import java.util.Enumeration;Let's see what it looks like compiling and executing this code:
import java.util.Hashtable;
public class putAll_hashtable {
public static void main(String[] s) {
Hashtable table = new Hashtable();
table.put("1", "val1");
table.put("2", "val2");
table.put("3", "val3");
Hashtable table2 = new Hashtable();
table2.put("4", "val4");
table2.put("5", "val5");
table2.put("6", "val6");
table2.putAll(table);
Enumeration e = table2.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " : " + table2.get(key));
}
}
}
6 : val6References:
5 : val5
4 : val4
3 : val3
2 : val2
1: val1
Tutorials Point: java.util.Hashtable.putAll() Method