We would like to know how to clone a java hashmap.
import java.util.HashMap; import java.util.Map; //from w w w .j a v a2 s . c o m public class Main { public static void main(String a[]) { HashMap hashMap = new HashMap(); HashMap hashMap1 = new HashMap(); hashMap.put(1, "One"); hashMap.put(2, "Two"); hashMap.put(3, "Three"); System.out.println("Original HashMap : " + hashMap); hashMap1 = (HashMap) hashMap.clone(); System.out.println("Copied HashMap : " + hashMap1); } }
The code above generates the following result.