Streams: Stream.concat joins two pipelines

Stream.concat is lazy and keeps encounter order, which makes it safer than copying both sources into a temporary list.

Code
var recent = Stream.of("2026-07", "2026-06");
var archive = Stream.of("2025-12", "2025-11");

List<String> all = Stream.concat(recent, archive).toList();
System.out.println(all);

List<String> three = Stream.of(Stream.of(1, 2), Stream.of(3), Stream.<Integer>of())
        .flatMap(Function.identity()).map(String::valueOf).toList();
System.out.println("flattened = " + three);
Output
[2026-07, 2026-06, 2025-12, 2025-11]
flattened = [1, 2, 3]
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-30