Not every join is on equality. When the right-hand list describes bands rather than keys, the lookup is a filter on a half-open interval - and findFirst makes the first matching band win.
record Tier(String name, double from, double upTo) {}
record Order(String ref, double amount) {}
var tiers = List.of(new Tier("bronze", 0, 100), new Tier("silver", 100, 500),
new Tier("gold", 500, Double.MAX_VALUE));
var orders = List.of(new Order("A-1", 40.0), new Order("A-2", 250.0), new Order("A-3", 900.0));
orders.forEach(o -> {
String tier = tiers.stream()
.filter(t -> o.amount() >= t.from() && o.amount() < t.upTo())
.map(Tier::name)
.findFirst()
.orElse("unbanded");
System.out.println(o.ref() + " " + o.amount() + " -> " + tier);
});
A-1 40.0 -> bronze
A-2 250.0 -> silver
A-3 900.0 -> gold
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