Join lists

suggest change

The following ways can be used for joining lists without modifying the source list(s).

First approach. It has more lines but is easy to understand

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

Second approach. It has one fewer line but is less readable.

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

Third approach. It requires the third-party Apache commons-collections library. union returns the joined list.

List<String> newList = ListUtils.union(listOne, listTwo);

Using streams, the same thing can be achieved by

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

References. Interface List

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents