在java中克隆HashMap实用程序

时间:2022-10-20 18:22:35

Is there java utility that does clone() method for HashMap such that it does copy of the map elements not just the map object (as the clone() in HashMap class)?

是否有java实用程序为HashMap做克隆()方法,以便它不仅复制map对象(如HashMap类中的克隆()),还复制map元素?

5 个解决方案

#1


11  

What about other objects referred to in the elements? How deep do you want your clone?

那么元素中引用的其他对象呢?你想要你的克隆体有多深?

If your map elements don't have any deep references and/or everything is Serializable, you can serialize the map via ObjectOutputStream into a ByteArrayOutputStream and then deserialize it right away.

如果您的映射元素没有任何深度引用或/或所有内容都是可序列化的,您可以通过ObjectOutputStream将映射序列化为ByteArrayOutputStream,然后立即反序列化它。

The only other alternative is to do it manually.

另一种选择是手动操作。

#2


1  

The SO question Deep clone utility recommendation is similar to this one, and has an answer that may be helpful to you.

深度克隆实用程序推荐的SO问题与此类似,并且有一个可能对您有帮助的答案。

To summarize, they recommend using the Cloning library from Google Code. From personal experience, it deep-copies HashMaps. It can even clone things that aren't Cloneable.

总之,他们建议使用谷歌代码中的克隆库。根据个人经验,它可以深入复制HashMaps。它甚至可以克隆那些不能克隆的东西。

#3


1  

Once you know your key/value pair elements are cloneable:

一旦您知道您的键/值对元素是可克隆的:

HashMap<Foo, Bar> map1 = populateHashmap();
HashMap<Foo, Bar> map2 = new HashMap<Foo, Bar>();

Set<Entry<Foo, Bar>> set1 = map1.entrySet();
for (Entry<Foo, Bar> e : l)
    map2.put(e.getKey().clone(), e.getValue().clone());

#4


0  

Take a look at the deepClone method at http://www.devdaily.com/java/jwarehouse/netbeans-src/db/libsrc/org/netbeans/lib/ddl/impl/SpecificationFactory.java.shtml . It is not generic, but it includes several built-in types (including HashMap itself, recursively), and can obviously be extended.

在http://www.devdaily.com/java/jwarehouse/netbes-src/db/libsrc/org/netbeans/libbeans/lib/lib/ddl/implationfactory.java.shtml查看deepClone方法。它不是通用的,但是它包含几个内置类型(递归地包括HashMap本身),并且显然可以扩展。

#5


0  

Often copy should be deep. Here is an example how to "deep copy"

通常拷贝应该是深的。下面是一个如何“深度复制”的例子

Map<Integer, ArrayList<Integer>>

code:

代码:

public static Map<Integer, ArrayList<Integer>> deepCopyMapIntList
        (Map<Integer, ArrayList<Integer>> original) {

    Map<Integer, ArrayList<Integer>> copy = new HashMap<>(original.size());

    for (int i : original.keySet()) {
        ArrayList<Integer> list = original.get(i);

        copy.put(i, (ArrayList<Integer>) list.clone());
    }
    return copy;
}

#1


11  

What about other objects referred to in the elements? How deep do you want your clone?

那么元素中引用的其他对象呢?你想要你的克隆体有多深?

If your map elements don't have any deep references and/or everything is Serializable, you can serialize the map via ObjectOutputStream into a ByteArrayOutputStream and then deserialize it right away.

如果您的映射元素没有任何深度引用或/或所有内容都是可序列化的,您可以通过ObjectOutputStream将映射序列化为ByteArrayOutputStream,然后立即反序列化它。

The only other alternative is to do it manually.

另一种选择是手动操作。

#2


1  

The SO question Deep clone utility recommendation is similar to this one, and has an answer that may be helpful to you.

深度克隆实用程序推荐的SO问题与此类似,并且有一个可能对您有帮助的答案。

To summarize, they recommend using the Cloning library from Google Code. From personal experience, it deep-copies HashMaps. It can even clone things that aren't Cloneable.

总之,他们建议使用谷歌代码中的克隆库。根据个人经验,它可以深入复制HashMaps。它甚至可以克隆那些不能克隆的东西。

#3


1  

Once you know your key/value pair elements are cloneable:

一旦您知道您的键/值对元素是可克隆的:

HashMap<Foo, Bar> map1 = populateHashmap();
HashMap<Foo, Bar> map2 = new HashMap<Foo, Bar>();

Set<Entry<Foo, Bar>> set1 = map1.entrySet();
for (Entry<Foo, Bar> e : l)
    map2.put(e.getKey().clone(), e.getValue().clone());

#4


0  

Take a look at the deepClone method at http://www.devdaily.com/java/jwarehouse/netbeans-src/db/libsrc/org/netbeans/lib/ddl/impl/SpecificationFactory.java.shtml . It is not generic, but it includes several built-in types (including HashMap itself, recursively), and can obviously be extended.

在http://www.devdaily.com/java/jwarehouse/netbes-src/db/libsrc/org/netbeans/libbeans/lib/lib/ddl/implationfactory.java.shtml查看deepClone方法。它不是通用的,但是它包含几个内置类型(递归地包括HashMap本身),并且显然可以扩展。

#5


0  

Often copy should be deep. Here is an example how to "deep copy"

通常拷贝应该是深的。下面是一个如何“深度复制”的例子

Map<Integer, ArrayList<Integer>>

code:

代码:

public static Map<Integer, ArrayList<Integer>> deepCopyMapIntList
        (Map<Integer, ArrayList<Integer>> original) {

    Map<Integer, ArrayList<Integer>> copy = new HashMap<>(original.size());

    for (int i : original.keySet()) {
        ArrayList<Integer> list = original.get(i);

        copy.put(i, (ArrayList<Integer>) list.clone());
    }
    return copy;
}