Conditions: do not modify 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);
Approach: ArrayLists can be joined in Java with the help of Collection. addAll() method. This method is called by the destination ArrayList and the other ArrayList is passed as the parameter to this method. This method appends the second ArrayList to the end of the first ArrayList.
To join elements of given ArrayList<String> arrayList with a delimiter string delimiter , use String. join() method. Call String. join() method and pass the delimiter string delimiter followed by the ArrayList<String> arrayList .
In Java 8:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()) .collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With