There are several ways to merge lists in Java
- You can call to
ArrayList(Collection<? extends E> c) - You can use the stream API, like
Stream.concat()orStream.of(listA, listB).forEach() - and more...
What would be the most memory and performance efficient way to merge two random access lists into a new random access list?
Try this to create an immutable list containing all the elements, by performing a shallow copy. Beware that changes to the source lists will be reflected in the resulting list (so the immutability in reality depends on the immutability / access to the input lists).
and
output
Considering that the original list is updated, it might be better to remove the field
sizeand do this: