Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #351 Create a zipped collection

Generate a "zipped" list z of pairs of elements from the lists a, b having the same length n.

The result z will contain n pairs.

z = zip(a, b)
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import static java.util.stream.Stream.of;
List f(Iterable ... c) {
    List<Iterator> a
        = of(c).map(Iterable::iterator)
               .toList();
    List<List> list = new ArrayList<>();
    List t;
    Iterator x;
    int i, n = a.size();
    boolean b;
    do {
        b = false;
        t = new ArrayList<>();
        for (i = 0; i != n; ++i)
            if ((x = a.get(i)).hasNext())
                b = t.add(x.next());
            else t.add(null);
        if (b) list.add(t);
    } while (b);
    return list;
}
z = a.zip(b)

New implementation...
< >
reilas