Multi-stream: compare budget against spend across two maps

Two maps keyed the same way still need a union of keys - a region with spend and no budget is precisely the row the report exists to show, and iterating either map alone hides it.

Code
var budget = Map.of("east", 1000.0, "west", 800.0);
var spend = Map.of("east", 1200.0, "north", 300.0);

Stream.concat(budget.keySet().stream(), spend.keySet().stream())
      .distinct()
      .sorted()
      .map(k -> {
          double b = budget.getOrDefault(k, 0.0);
          double s = spend.getOrDefault(k, 0.0);
          return k + " budget=" + b + " spend=" + s + (s > b ? "  OVER by " + (s - b) : "");
      })
      .forEach(System.out::println);
Output
east budget=1000.0 spend=1200.0  OVER by 200.0
north budget=0.0 spend=300.0  OVER by 300.0
west budget=800.0 spend=0.0
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