Join lists
suggest changeThe 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents