条件:不修改原有列表;只使用JDK,没有外部库。单行程序或JDK 1.3版本的加分项。

有没有比这更简单的方法:

List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);

当前回答

import java.util.AbstractList;
import java.util.List;


/**
 * The {@code ConcatList} is a lightweight view of two {@code List}s.
 * <p>
 * This implementation is <em>not</em> thread-safe even though the underlying lists can be.
 * 
 * @param <E>
 *            the type of elements in this list
 */
public class ConcatList<E> extends AbstractList<E> {

    /** The first underlying list. */
    private final List<E> list1;
    /** The second underlying list. */
    private final List<E> list2;

    /**
     * Constructs a new {@code ConcatList} from the given two lists.
     * 
     * @param list1
     *            the first list
     * @param list2
     *            the second list
     */
    public ConcatList(final List<E> list1, final List<E> list2) {
        this.list1 = list1;
        this.list2 = list2;
    }

    @Override
    public E get(final int index) {
        return getList(index).get(getListIndex(index));
    }

    @Override
    public E set(final int index, final E element) {
        return getList(index).set(getListIndex(index), element);
    }

    @Override
    public void add(final int index, final E element) {
        getList(index).add(getListIndex(index), element);
    }

    @Override
    public E remove(final int index) {
        return getList(index).remove(getListIndex(index));
    }

    @Override
    public int size() {
        return list1.size() + list2.size();
    }

    @Override
    public boolean contains(final Object o) {
        return list1.contains(o) || list2.contains(o);
    }

    @Override
    public void clear() {
        list1.clear();
        list2.clear();
    }

    /**
     * Returns the index within the corresponding list related to the given index.
     * 
     * @param index
     *            the index in this list
     * 
     * @return the index of the underlying list
     */
    private int getListIndex(final int index) {
        final int size1 = list1.size();
        return index >= size1 ? index - size1 : index;
    }

    /**
     * Returns the list that corresponds to the given index.
     * 
     * @param index
     *            the index in this list
     * 
     * @return the underlying list that corresponds to that index
     */
    private List<E> getList(final int index) {
        return index >= list1.size() ? list2 : list1;
    }

}

其他回答

几乎所有的回答都建议使用数组列表。

List<String> newList = new LinkedList<>(listOne);
newList.addAll(listTwo);

更喜欢使用LinkedList进行高效的添加操作。

ArrayList add是O(1)平摊,但最坏情况是O(n),因为数组必须调整大小和复制。 而LinkedList add总是常数O(1)。

更多信息https://stackoverflow.com/a/322742/311420

稍微简单:

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);

这很简单,只有一行,但是会将listTwo的内容添加到listOne。你真的需要把内容放到第三个列表里吗?

Collections.addAll(listOne, listTwo.toArray());

您的要求之一是保存原始的清单。如果您创建一个新列表并使用addAll(),那么您实际上是将列表中对象的引用数量增加了一倍。如果您的列表非常大,这可能会导致内存问题。

如果不需要修改连接的结果,可以使用自定义列表实现来避免这种情况。自定义实现类不止一行,显然…但是使用它是简短而甜蜜的。

CompositeUnmodifiableList.java:

public class CompositeUnmodifiableList<E> extends AbstractList<E> {

    private final List<? extends E> list1;
    private final List<? extends E> list2;

    public CompositeUnmodifiableList(List<? extends E> list1, List<? extends 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();
    }
}

用法:

List<String> newList = new CompositeUnmodifiableList<String>(listOne,listTwo);

在Java 8中(另一种方式):

List<?> newList = 
Stream.of(list1, list2).flatMap(List::stream).collect(Collectors.toList());