如何在Java中加入两个列表?
本文翻译自:How do I join two lists in Java?
Conditions: do not modifiy the original lists; 条件:不修改原始列表; JDK only, no external libraries. 仅限JDK,没有外部库。 Bonus points for a one-liner or a JDK 1.3 version. 单行或JDK 1.3版本的奖励积分。
Is there a simpler way than: 有没有比以下更简单的方法:
List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
#1楼
参考:https://stackoom.com/question/nJP/如何在Java中加入两个列表
#2楼
The smartest in my opinion: 我认为最聪明的是:
/**
* @param smallLists
* @return one big list containing all elements of the small ones, in the same order.
*/
public static <E> List<E> concatenate (final List<E> ... smallLists)
{
final ArrayList<E> bigList = new ArrayList<E>();
for (final List<E> list: smallLists)
{
bigList.addAll(list);
}
return bigList;
}
#3楼
Not simpler, but without resizing overhead: 不简单,但没有调整开销:
List<String> newList = new ArrayList<>(listOne.size() + listTwo.size());
newList.addAll(listOne);
newList.addAll(listTwo);
#4楼
One of your requirements is to preserve the original lists. 您的一个要求是保留原始列表。 If you create a new list and use addAll()
, you are effectively doubling the number of references to the objects in your lists. 如果创建新列表并使用addAll()
,则实际上会使列表中对象的引用数增加一倍。 This could lead to memory problems if your lists are very large. 如果列表非常大,这可能会导致内存问题。
If you don't need to modify the concatenated result, you can avoid this using a custom list implementation. 如果您不需要修改连接结果,则可以使用自定义列表实现来避免这种情况。 The custom implementation class is more than one line, obviously...but using it is short and sweet. 自定义实现类不止一行,显然......但使用它很简短。
CompositeUnmodifiableList.java: CompositeUnmodifiableList.java:
public class CompositeUnmodifiableList<E> extends AbstractList<E> {
private final List<E> list1;
private final List<E> list2;
public CompositeUnmodifiableList(List<E> list1, List<E> list2) {
this.list1 = list1;
this.list2 = list2;
}
@Override
public E get(int index) {
if (index < list1.size()) {
return list1.get(index);
}
return list2.get(index-list1.size());
}
@Override
public int size() {
return list1.size() + list2.size();
}
}
Usage: 用法:
List<String> newList = new CompositeUnmodifiableList<String>(listOne,listTwo);
#5楼
public class TestApp {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hi");
Set<List<String>> bcOwnersList = new HashSet<List<String>>();
List<String> bclist = new ArrayList<String>();
List<String> bclist1 = new ArrayList<String>();
List<String> object = new ArrayList<String>();
object.add("BC11");
object.add("C2");
bclist.add("BC1");
bclist.add("BC2");
bclist.add("BC3");
bclist.add("BC4");
bclist.add("BC5");
bcOwnersList.add(bclist);
bcOwnersList.add(object);
bclist1.add("BC11");
bclist1.add("BC21");
bclist1.add("BC31");
bclist1.add("BC4");
bclist1.add("BC5");
List<String> listList= new ArrayList<String>();
for(List<String> ll : bcOwnersList){
listList = (List<String>) CollectionUtils.union(listList,CollectionUtils.intersection(ll, bclist1));
}
/*for(List<String> lists : listList){
test = (List<String>) CollectionUtils.union(test, listList);
}*/
for(Object l : listList){
System.out.println(l.toString());
}
System.out.println(bclist.contains("BC"));
}
}
#6楼
In Java 8: 在Java 8中:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
.collect(Collectors.toList());