Java ProgramsCollectionsMerge Two Lists

Merge Two Lists in Java

beginner·  Collections  ·  Conversions

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.

Input
list1 = [Apple, Banana], list2 = [Cherry, Date]
Output
[Apple, Banana, Cherry, Date]

Java Program

Java
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

[Apple, Banana, Cherry, Date]

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.

How It Works
  1. 1new ArrayList<>(list1) creates a fresh list already containing every element of list1, without disturbing list1 itself.
  2. 2merged.addAll(list2) appends every element of list2 onto the end of merged, in list2's own order.
  3. 3Both original lists, list1 and list2, are left unchanged — only the new merged list is modified.
  4. 4Printing merged shows every element from list1 followed by every element from list2.
Merging [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.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: Every element from both lists, of sizes n and m, is copied into the merged list exactly once.

Key Concepts

ArrayListaddAll()List concatenation

Approach 2: Java 8

Java
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

[Apple, Banana, Cherry, Date]

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.

How It Works
  1. 1list1.stream() and list2.stream() open a stream over each original list.
  2. 2Stream.concat(...) joins them into a single stream that yields every element of the first stream, then every element of the second.
  3. 3.collect(Collectors.toList()) gathers that combined sequence into a new List, in the same order it was streamed.
  4. 4Like the manual version, both original lists are left untouched — the streams only read from them.
Concatenating streams over [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.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: concat() still has to stream every element from both lists once, and collect() builds a new list holding all of them.

Key Concepts

Stream.concat()Collectors.toList()

Related Programs