Join lists

suggest change

Following ways can be used for joining lists without modifying source list(s).

First approach. Has more lines but easy to understand

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

Second approach. Has one less line but less readable.

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

Third approach. Requires third party Apache commons-collections library.

ListUtils.union(listOne,listTwo);

Using Streams the same 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