Multi-stream: full outer join over the union of both key sets

Neither list drives a full outer join - the union of their keys does. Build that union in a TreeSet so the report comes out in a stable order rather than whatever the hash buckets decide.

Code
record Forecast(String region, int planned) {}
record Actual(String region, int sold) {}

var forecast = List.of(new Forecast("east", 100), new Forecast("north", 40));
var actual = List.of(new Actual("north", 55), new Actual("west", 20));

Map<String, Forecast> f = forecast.stream().collect(Collectors.toMap(Forecast::region, v -> v));
Map<String, Actual> a = actual.stream().collect(Collectors.toMap(Actual::region, v -> v));

var regions = new TreeSet<String>();
regions.addAll(f.keySet());
regions.addAll(a.keySet());

regions.forEach(r -> System.out.println(r
        + " planned=" + (f.containsKey(r) ? f.get(r).planned() : "-")
        + " sold=" + (a.containsKey(r) ? a.get(r).sold() : "-")));
Output
east planned=100 sold=-
north planned=40 sold=55
west planned=- sold=20
Advertisement
More in JAVA

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-09-20

© Java Coding Hub · About · Contact · Privacy · Terms