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

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

问题描述

我有 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 isn大到足以容纳 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);

中创建一个浅层副本 code> b 。所有元素都将存在于 b 中,其顺序与它们在 a 中的顺序完全相同(假设它有一个订单)。

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

还会在 a 内创建一个浅表副本 b 。如果第一个参数 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.

要创建深层副本,列表,通过任何一种机制,都必须具有基础类型的复杂知识。对于 String s,它们在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 Collections列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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