Merge Two Lists in Java
Problem
Merging two Lists means building one List that contains every element from both, in order — Java's addAll() does this in a single call instead of looping over the second list by hand.
Given two Lists, combine them into a single List containing every element from both.
Java Program
import java.util.ArrayList;
import java.util.List;
public class MergeTwoLists {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>(List.of("Apple", "Banana"));
List<String> list2 = new ArrayList<>(List.of("Cherry", "Date"));
List<String> merged = new ArrayList<>(list1); // copy, so list1 itself stays untouched
merged.addAll(list2); // appends every element of list2 onto merged
System.out.println(merged);
}
}Output
Core Logic
Starting a new list from the first list's elements, then calling addAll() with the second list, appends every element from both in one step.
- 1
new ArrayList<>(list1)creates a fresh list already containing every element oflist1, without disturbinglist1itself. - 2
merged.addAll(list2)appends every element oflist2onto the end ofmerged, inlist2's own order. - 3Both original lists,
list1andlist2, are left unchanged — only the newmergedlist is modified. - 4Printing
mergedshows every element fromlist1followed by every element fromlist2.
[Apple, Banana] and [Cherry, Date] produces [Apple, Banana, Cherry, Date], in that order.Key Point: Copying list1 into a new list first — rather than calling addAll() directly on list1 — keeps the original list1 reference untouched, which matters if other code still holds onto it.
Why: Every element from both lists, of sizes n and m, is copied into the merged list exactly once.
Key Concepts
Approach 2: Java 8
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MergeTwoListsStream {
public static void main(String[] args) {
List<String> list1 = List.of("Apple", "Banana");
List<String> list2 = List.of("Cherry", "Date");
// Joins both streams end to end, then collects the combined sequence
List<String> merged = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println(merged);
}
}
Output
Core Logic
Stream.concat() joins two streams end to end, so collecting the concatenated result reproduces the same merged list without an explicit copy-then-addAll step.
- 1
list1.stream()andlist2.stream()open a stream over each original list. - 2
Stream.concat(...)joins them into a single stream that yields every element of the first stream, then every element of the second. - 3
.collect(Collectors.toList())gathers that combined sequence into a new List, in the same order it was streamed. - 4Like the manual version, both original lists are left untouched — the streams only read from them.
[Apple, Banana] and [Cherry, Date] yields the same combined sequence, collected into [Apple, Banana, Cherry, Date].Key Point: Stream.concat() is the more natural choice when the two lists are already the result of earlier stream operations — otherwise, the plain addAll() version is simpler to read for two ordinary lists.
Why: concat() still has to stream every element from both lists once, and collect() builds a new list holding all of them.