Zip Two Lists Pairwise

Zip two lists by index into record pairs: the shorter side caps the length so uneven inputs clip instead of throwing IndexOutOfBounds.

JavaData Integrity

Code

snippet
import java.util.List;
import java.util.stream.IntStream;

public class PairwiseZip {

    record Quote(String symbol, String price) {}

    // zip two lists pairwise:按索引配对,较短一侧决定长度,不会越界
    static List<Quote> zip(List<String> symbols, List<String> prices) {
        return IntStream.range(0, Math.min(symbols.size(), prices.size()))
                .mapToObj(i -> new Quote(symbols.get(i), prices.get(i)))
                .toList();
    }

    public static void main(String[] args) {
        List<Quote> quotes = zip(
                List.of("AAPL", "MSFT", "NVDA"),
                List.of("180.21", "415.50", "126.30"));
        quotes.forEach(System.out::println);
        System.out.println("count = " + quotes.size());

        // 长度不等:较短一侧封顶,多余元素被裁掉
        List<String> shortSide = List.of("AAPL");
        List<Quote> clipped = zip(shortSide, quotes.stream().map(Quote::symbol).toList());
        System.out.println("clipped count = " + clipped.size());
    }
}

How the pieces connect

Zip Two Lists Pairwise keeps everything in the classes PairwiseZip and Quote and the functions zip and main.

The pipeline is a stream: source, lazy intermediate steps, one terminal collector.

Caveats

  • No lockfile entry, no version pinning — the file needs nothing beyond the runtime.

Applying it

The entry-point signature is static List<Quote> zip(List<String> symbols, List<String> prices)

The run prints count =, clipped count = — each prefix names the step that produced it.

Treat main as the orchestrator calling zip in a fixed order.

Statements that matter

List<Quote> clipped = zip(shortSide, quotes.stream().map(Quote::symbol).toList());
return IntStream.range(0, Math.min(symbols.size(), prices.size()))
        .mapToObj(i -> new Quote(symbols.get(i), prices.get(i)))
        .toList();

Interactive Tools for This Topic

FAQ

What does Zip Two Lists Pairwise print when run?
Invoke the file as-is — the `count =`, `clipped count =` prefixes identify which step printed each line.