package com.baeldung.guava.zip; import com.google.common.collect.Streams; import org.jooq.lambda.Seq; import org.jooq.lambda.tuple.Tuple2; import org.junit.Before; import org.junit.Test; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; import static org.junit.Assert.assertEquals; public class ZipCollectionTest { private List names; private List ages; private List expectedOutput; @Before public void setUp() throws Exception { names = Arrays.asList("John", "Jane", "Jack", "Dennis"); ages = Arrays.asList(24, 25, 27); expectedOutput = Arrays.asList("John:24", "Jane:25", "Jack:27"); } @Test public void zipCollectionUsingGuava21() { List output = Streams .zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age) .collect(Collectors.toList()); assertEquals(output, expectedOutput); } @Test public void zipCollectionUsingIntStream() { List output = IntStream .range(0, Math.min(names.size(), ages.size())) .mapToObj(i -> names.get(i) + ":" + ages.get(i)) .collect(Collectors.toList()); assertEquals(output, expectedOutput); } @Test public void zipCollectionUsingJool() { Seq output = Seq .of("John", "Jane", "Jack") .zip(Seq.of(24, 25, 27), (x, y) -> x + ":" + y); assertEquals(output.toList(), expectedOutput); } @Test public void zipCollectionUsingJoolTuple() { Seq> output = Seq .of("John", "Jane", "Dennis") .zip(Seq.of(24, 25, 27)); Tuple2 element1 = new Tuple2("John", 24); Tuple2 element2 = new Tuple2("Jane", 25); Tuple2 element3 = new Tuple2("Dennis", 27); List expectedOutput = Arrays.asList(element1, element2, element3); assertEquals(output.collect(Collectors.toList()), expectedOutput); } @Test public void zipCollectionUsingJoolWithIndex() { Seq> output = Seq .of("John", "Jane", "Dennis") .zipWithIndex(); Tuple2 element1 = new Tuple2<>("John", 0L); Tuple2 element2 = new Tuple2<>("Jane", 1L); Tuple2 element3 = new Tuple2<>("Dennis", 2L); List expectedOutput = Arrays.asList(element1, element2, element3); assertEquals(output.collect(Collectors.toList()), expectedOutput); } }