如何在 Java 中加入两个列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/189559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I join two lists in Java?
提问by Robert Atkins
Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.
条件:不修改原列表;只有 JDK,没有外部库。单行或 JDK 1.3 版本的奖励积分。
Is there a simpler way than:
有没有比以下更简单的方法:
List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
回答by Tim
Slightly simpler:
稍微简单一点:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
回答by AdamC
Off the top of my head, I can shorten it by one line:
在我的头顶,我可以把它缩短一行:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
回答by Jorn
A little shorter would be:
稍微短一点是:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
回答by volley
Probably not simpler, but intriguing and ugly:
可能不简单,但有趣和丑陋:
List<String> newList = new ArrayList<String>() { { addAll(listOne); addAll(listTwo); } };
Don't use it in production code... ;)
不要在生产代码中使用它...;)
回答by deterb
You can do a oneliner if the target list is predeclared.
如果预先声明了目标列表,您可以执行 oneliner。
(newList = new ArrayList<String>(list1)).addAll(list2);
回答by Dave Cheney
You could do it with a static import and a helper class
您可以使用静态导入和辅助类来完成
nbthe generification of this class could probably be improved
注意这个类的generification很可能得到改善
public class Lists {
private Lists() { } // can't be instantiated
public static List<T> join(List<T>... lists) {
List<T> result = new ArrayList<T>();
for(List<T> list : lists) {
result.addAll(list);
}
return results;
}
}
Then you can do things like
然后你可以做这样的事情
import static Lists.join;
List<T> result = join(list1, list2, list3, list4);
回答by Dov Wasserman
I can't improve on the two-liner in the general case without introducing your own utility method, but if you do have lists of Strings and you're willing to assume those Strings don't contain commas, you can pull this long one-liner:
在不引入您自己的实用程序方法的情况下,我无法改进一般情况下的两行代码,但是如果您确实有字符串列表并且您愿意假设这些字符串不包含逗号,则可以拉长一个-衬垫:
List<String> newList = new ArrayList<String>(Arrays.asList((listOne.toString().subString(1, listOne.length() - 1) + ", " + listTwo.toString().subString(1, listTwo.length() - 1)).split(", ")));
If you drop the generics, this should be JDK 1.4 compliant (though I haven't tested that). Also not recommended for production code ;-)
如果你放弃泛型,这应该是 JDK 1.4 兼容的(虽然我还没有测试过)。也不推荐用于生产代码;-)
回答by ddimitrov
I'm not claiming that it's simple, but you mentioned bonus for one-liners ;-)
我并不是说这很简单,但是您提到了单线奖金;-)
Collection mergedList = Collections.list(new sun.misc.CompoundEnumeration(new Enumeration[] {
new Vector(list1).elements(),
new Vector(list2).elements(),
...
}))
回答by alex
Use a Helper class.
使用 Helper 类。
I suggest:
我建议:
public static <E> Collection<E> addAll(Collection<E> dest, Collection<? extends E>... src) {
for(Collection<? extends E> c : src) {
dest.addAll(c);
}
return dest;
}
public static void main(String[] args) {
System.out.println(addAll(new ArrayList<Object>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));
// does not compile
// System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));
System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList(4, 5, 6)));
}
回答by Guillermo
You could use the Apache commons-collectionslibrary:
您可以使用Apache commons-collections库:
List<String> newList = ListUtils.union(list1, list2);