如何复制 Java 集合列表 [英] How to copy Java Collections list

查看:31
本文介绍了如何复制 Java 集合列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList 并且我想准确地复制它.我尽可能使用实用程序类,假设有人花了一些时间使其正确.很自然地,我最终得到了包含复制方法的 Collections 类.

I have an ArrayList and I want to copy it exactly. I use utility classes when possible on the assumption that someone spent some time making it correct. So naturally, I end up with the Collections class which contains a copy method.

假设我有以下内容:

List<String> a = new ArrayList<String>();
a.add("a");
a.add("b");
a.add("c");
List<String> b = new ArrayList<String>(a.size());

Collections.copy(b,a);

这失败了,因为基本上它认为b 不足以容纳a.是的,我知道 b 的大小为 0,但现在应该足够大了吧?如果非要先填b,那Collections.copy()在我心里就变成完全没用的函数了.那么,除了编写复制函数(我现在要做的)之外,还有其他合适的方法可以做到这一点吗?

This fails because basically it thinks b isn't big enough to hold a. Yes I know b has size 0, but it should be big enough now shouldn't it? If I have to fill b first, then Collections.copy() becomes a completely useless function in my mind. So, except for programming a copy function (which I'm going to do now) is there a proper way to do this?

推荐答案

调用

List<String> b = new ArrayList<String>(a);

b 中创建a 的浅拷贝.所有元素都将按照它们在 a 中的完全相同的顺序存在于 b 中(假设它有一个顺序).

creates a shallow copy of a within b. All elements will exist within b in the exact same order that they were within a (assuming it had an order).

同样,调用

// note: instantiating with a.size() gives `b` enough capacity to hold everything
List<String> b = new ArrayList<String>(a.size());
Collections.copy(b, a);

还在b 中创建a 的浅拷贝.如果第一个参数 b 没有足够的 容量(不是大小)来包含所有 a 的元素,那么它将抛出IndexOutOfBoundsException.期望 Collections.copy 工作,如果有,则抛出该异常.要求预先分配复制的集合是一种优化(b),但我通常认为该功能不值得,因为需要检查基于构造函数的替代方案,如上所示没有奇怪的副作用.

also creates a shallow copy of a within b. If the first parameter, b, does not have enough capacity (not size) to contain all of a's elements, then it will throw an IndexOutOfBoundsException. The expectation is that no allocations will be required by Collections.copy to work, and if any are, then it throws that exception. It's an optimization to require the copied collection to be preallocated (b), but I generally do not think that the feature is worth it due to the required checks given the constructor-based alternatives like the one shown above that have no weird side effects.

要创建深层副本,List,通过任一机制,都必须具有底层类型的复杂知识.对于 Strings,它在 Java(和 .NET 中)是不可变的,你甚至不需要深拷贝.对于 MySpecialObject,您需要知道如何对其进行深拷贝,这不是通用操作.

To create a deep copy, the List, via either mechanism, would have to have intricate knowledge of the underlying type. In the case of Strings, which are immutable in Java (and .NET for that matter), you don't even need a deep copy. In the case of MySpecialObject, you need to know how to make a deep copy of it and that is not a generic operation.

注意:最初接受的答案是 Google 中 Collections.copy 的最高结果,正如评论中指出的那样,这是完全错误的.

Note: The originally accepted answer was the top result for Collections.copy in Google, and it was flat out wrong as pointed out in the comments.

这篇关于如何复制 Java 集合列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆