How do I join two lists in Java?

Robert Atkins picture Robert Atkins · Oct 10, 2008 · Viewed 905k times · Source

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);

Answer

Dale Emery picture Dale Emery · Sep 8, 2013

In Java 8:

List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
                             .collect(Collectors.toList());