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);
You can create your generic Java 8 utility method to concat any number of lists.
You could do it with a static import and a helper class
nb the generification of this class could probably be improved
Then you can do things like
In Java 8 (the other way):
Found this question looking to concatenate arbitrary amount of lists, not minding external libraries. So, perhaps it will help someone else:
Useful if you want to apply the same logic to a number of different collections in one for().
Another Java 8 one-liner:
As a bonus, since
Stream.of()
is variadic, you may concatenate as many lists as you like.Java 8 version with support for joining by object key: