gpt4 book ai didi

java - 将 Hashmap 分配给 Hashmap

转载 作者:太空狗 更新时间:2023-10-29 22:36:14 24 4
gpt4 key购买 nike

我有一个散列图,我想复制它以供其他用途。但是每当我复制它并重新使用它时,它也会改变原来的。这是为什么?

    do {
Map<Integer, Map<String, Object>> map1 = originalMap;
//at the second iteration originalMap is the same as map1 of the last iteration,
//eventhough the change was nog accepted;
//do something with map1 (change value);
if(change is accepted) {
originalMap = map1;
}
} while(iteration < 10);

提前致谢

    public static <Integer,String, Schedule>Map<Integer, Map<String, Schedule>> deepCopy(Map<Integer, Map<String, Schedule>> original) {
Map<Integer, Map<String, Schedule>> copy = new HashMap<Integer, Map<String, Schedule>>();

for (Map.Entry<Integer, Map<String, Schedule>> entry : original.entrySet()) {
copy.put(entry.getKey(), deepCopy2(entry.getValue()));
}
return copy;
}

public static <String, Schedule>Map<String, Schedule> deepCopy2(Map<String, Schedule> original) {
Map<String, Schedule> copy = new HashMap<String, Schedule>();
for (Map.Entry<String, Schedule> entry : original.entrySet()) {
copy.put(entry.getKey(), entry.getValue());
}

return copy;
}

最佳答案

您所做的不是创建 map 的副本,而是创建对 map 的引用。当两个引用指向同一个对象时,对一个的更改将反射(reflect)在另一个中。

解决方案 1:如果这是从某种简单类型到另一种类型的 Map,您可以改为这样做:

Map<SomeType, OtherType> map1 = new HashMap<SomeType, OtherType>(original); 

这叫做 Copy Constructor .几乎所有标准的 Collection 和 Map 实现都有一个,它通常是克隆简单结构的最简单方法。只要 SomeTypeOtherTypeimmutable 就可以正常工作(例如 Integer 和其他 Number 类型、BooleanString,但不包括 Collections、Dates、Maps、Arrays 等.)

如果没有,正如其他回答者和评论者所指出的,您还需要复制 map 值。

解决方案 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 here
Map<Integer, Map<String, Object>> copy = deepCopy(original);

更新

我已经破解了一个类,该类对 map 、集合和数组(原始的和其他的)执行深度克隆。用法:

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;
}
}

关于java - 将 Hashmap 分配给 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11296490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com