How do I join two lists in Java?
Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.
Is there a simpler way than:
List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
在我头顶,我可以缩短一行:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
在Java 8中:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
.collect(Collectors.toList());
你可以使用Apache commons-collections库:
List<String> newList = ListUtils.union(list1,list2);
链接地址: http://www.djcxy.com/p/82396.html
上一篇: 什么是Java中的Double Brace初始化?
下一篇: 我如何在Java中加入两个列表?