How do I join two lists in Java?

后端 未结 30 2153
旧巷少年郎
旧巷少年郎 2020-11-22 14:36

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:

30条回答
  •  无人共我
    2020-11-22 15:16

    Almost of answers suggest to use an ArrayList.

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

    Prefer to use a LinkedList for efficient add operations.

    ArrayList add is O(1) amortized, but O(n) worst-case since the array must be resized and copied. While LinkedList add is always constant O(1).

    more infos https://stackoverflow.com/a/322742/311420

提交回复
热议问题