Multi-stream: zip two parallel lists by streaming the index

The JDK has no zip(). Stream the index range instead and read both lists through it - and clamp to the shorter list, because two feeds that are supposed to line up eventually will not.

Code
var skus  = List.of("S1", "S2", "S3");
var stock = List.of(12, 0, 7);

IntStream.range(0, Math.min(skus.size(), stock.size()))
         .mapToObj(i -> skus.get(i) + " -> " + stock.get(i)
                 + (stock.get(i) == 0 ? "  (out of stock)" : ""))
         .forEach(System.out::println);
Output
S1 -> 12
S2 -> 0  (out of stock)
S3 -> 7
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