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:
Here is a java 8 solution using two lines:
List
Be aware that this method should not be used if
newList
is not known and it may already be shared with other threadsnewList
is a parallel stream and access to newList
is not synchronized or threadsafedue to side effect considerations.
Both of the above conditions do not apply for the above case of joining two lists, so this is safe.
Based on this answer to another question.