Multi-stream: best and worst quote across two venues

Concatenating two feeds and reducing gives the cross-venue answer in one pass per question. reduce with a hand-written tie-break and max with a Comparator are the same operation written two ways.

Code
record Quote(String venue, double price) {}

var nyse = List.of(new Quote("NYSE", 101.2), new Quote("NYSE", 100.8));
var bats = List.of(new Quote("BATS", 100.5), new Quote("BATS", 101.9));

var cheapest = Stream.concat(nyse.stream(), bats.stream())
        .reduce((x, y) -> x.price() <= y.price() ? x : y)
        .orElseThrow();
var dearest = Stream.concat(nyse.stream(), bats.stream())
        .max(Comparator.comparingDouble(Quote::price))
        .orElseThrow();

System.out.println("cheapest: " + cheapest.venue() + " " + cheapest.price());
System.out.println("dearest : " + dearest.venue() + " " + dearest.price());
Output
cheapest: BATS 100.5
dearest : BATS 101.9
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