Approach 1 — Using Stream.concat
List<T> merged = Stream.concat(a.stream(), b.stream())
.collect(Collectors.toList());
Approach 2 — Using Stream.of + flatMap
List<T> merged2 = Stream.of(a, b)
.flatMap(Collection::stream)
.toList();
Explanation:
Stream.concatis ideal for merging two streams.flatMapgeneralizes to merging multiple lists or collections in a single step.
