您要做的不是创建地图的副本,而是创建地图的副本。当两个引用指向同一对象时,对一个对象的更改将在另一个对象中反映出来。
解决方案1:如果这是从某种简单类型到另一种类型的Map,则应改为:
Map<SomeType, OtherType> map1 = new HashMap<SomeType, OtherType>(original);
这称为复制构造函数。几乎所有标准的Collection和Map实现都有一个,通常是克隆简单结构的最简单方法。这将正常工作,只要
SomeType和
OtherType是不变的(例如,
Integer和其他
Number类型
Boolean,
String但不集合,日期,地图,阵列等)
否则,正如其他答复者和评论者所指出的那样,您还需要复制地图值。
解决方案2:这是一个快速而又肮脏的版本,应该是安全的:
Map<Integer, Map<String, Object>> original=new HashMap<Integer, Map<String,Object>>();Map<Integer, Map<String, Object>> copy = new HashMap<Integer, Map<String, Object>>();for(Entry<Integer, Map<String, Object>> entry : original.entrySet()){ copy.put(entry.getKey(), new HashMap<String, Object>(entry.getValue()));}但是实际上,我喜欢Hunter提供深层复制方法的想法。因此,这里是解决方案3:使用通用参数的我自己的版本:
public static <K1, K2, V> Map<K1, Map<K2, V>> deepCopy( Map<K1, Map<K2, V>> original){ Map<K1, Map<K2, V>> copy = new HashMap<K1, Map<K2, V>>(); for(Entry<K1, Map<K2, V>> entry : original.entrySet()){ copy.put(entry.getKey(), new HashMap<K2, V>(entry.getValue())); } return copy;}您可以这样称呼它:
Map<Integer, Map<String, Object>> original=new HashMap<Integer, Map<String,Object>>();// do stuff hereMap<Integer, Map<String, Object>> copy = deepCopy(original);
更新资料
我在一起学习了一个类,该类对Maps,Collections和Array(原始和其他)执行深度克隆。用法:
Something clone = DeepClone.deepClone(original);
这里是:
public final class DeepClone { private DeepClone(){} public static <X> X deepClone(final X input) { if (input == null) { return input; } else if (input instanceof Map<?, ?>) { return (X) deepCloneMap((Map<?, ?>) input); } else if (input instanceof Collection<?>) { return (X) deepCloneCollection((Collection<?>) input); } else if (input instanceof Object[]) { return (X) deepCloneObjectArray((Object[]) input); } else if (input.getClass().isArray()) { return (X) clonePrimitiveArray((Object) input); } return input; } private static Object clonePrimitiveArray(final Object input) { final int length = Array.getLength(input); final Object copy = Array.newInstance(input.getClass().getComponentType(), length); // deep clone not necessary, primitives are immutable System.arraycopy(input, 0, copy, 0, length); return copy; } private static <E> E[] deepCloneObjectArray(final E[] input) { final E[] clone = (E[]) Array.newInstance(input.getClass().getComponentType(), input.length); for (int i = 0; i < input.length; i++) { clone[i] = deepClone(input[i]); } return clone; } private static <E> Collection<E> deepCloneCollection(final Collection<E> input) { Collection<E> clone; // this is of course far from comprehensive. extend this as needed if (input instanceof linkedList<?>) { clone = new linkedList<E>(); } else if (input instanceof SortedSet<?>) { clone = new TreeSet<E>(); } else if (input instanceof Set) { clone = new HashSet<E>(); } else { clone = new ArrayList<E>(); } for (E item : input) { clone.add(deepClone(item)); } return clone; } private static <K, V> Map<K, V> deepCloneMap(final Map<K, V> map) { Map<K, V> clone; // this is of course far from comprehensive. extend this as needed if (map instanceof linkedHashMap<?, ?>) { clone = new linkedHashMap<K, V>(); } else if (map instanceof TreeMap<?, ?>) { clone = new TreeMap<K, V>(); } else { clone = new HashMap<K, V>(); } for (Entry<K, V> entry : map.entrySet()) { clone.put(deepClone(entry.getKey()), deepClone(entry.getValue())); } return clone; }}


