From 19577503828fcaa324731dcee198414455e21daa Mon Sep 17 00:00:00 2001 From: Devendra Tiwari Date: Sun, 9 Jul 2017 01:50:49 +0530 Subject: [PATCH] How to zip two collections in Java? (#2176) * How to zip two collections in Java? How to zip two collections in Java? | http://jira.baeldung.com/browse/BAEL-780 * Updated pom to add jool dependency * Added Unit Tests of Zip Collections * Moved files to libraries folder * Fixed Compilation Error * Removed jool dependency as code has been moved to libraries * Removed jool property attribute --- core-java/pom.xml | 2 +- libraries/pom.xml | 6 ++ .../baeldung/zip/ZipCollectionExample.java | 40 +++++++++ .../com/baeldung/zip/ZipCollectionTest.java | 85 +++++++++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 libraries/src/main/java/com/baeldung/zip/ZipCollectionExample.java create mode 100644 libraries/src/test/java/com/baeldung/zip/ZipCollectionTest.java diff --git a/core-java/pom.xml b/core-java/pom.xml index 84a56c8bc7..ee0d6b4b4a 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -417,4 +417,4 @@ 2.19.1 - \ No newline at end of file + diff --git a/libraries/pom.xml b/libraries/pom.xml index a3b78f1695..0373d54429 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -349,6 +349,12 @@ java-lsh ${java-lsh.version} + + + com.google.guava + guava + 21.0 + au.com.dius pact-jvm-consumer-junit_2.11 diff --git a/libraries/src/main/java/com/baeldung/zip/ZipCollectionExample.java b/libraries/src/main/java/com/baeldung/zip/ZipCollectionExample.java new file mode 100644 index 0000000000..3df9baad69 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/zip/ZipCollectionExample.java @@ -0,0 +1,40 @@ +package com.baeldung.zip; + +import com.google.common.collect.Streams; +import org.jooq.lambda.Seq; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.IntStream; + +public class ZipCollectionExample { + static List names = Arrays.asList("John", "Jane", "Jack", "Dennis"); + + static List ages = Arrays.asList(24, 25, 27); + + public static void main(String[] args) { + // Using Streams API from Guava 21 + Streams + .zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age) + .forEach(System.out::println); + + // Using native Java 8 Int Stream + IntStream + .range(0, Math.min(names.size(), ages.size())) + .mapToObj(i -> names.get(i) + ":" + ages.get(i)) + .forEach(System.out::println); + + // Using jOOL + Seq + .of("John", "Jane", "Dennis") + .zip(Seq.of(24, 25, 27)); + + Seq + .of("John", "Jane", "Dennis") + .zip(Seq.of(24, 25, 27), (x, y) -> x + ":" + y); + + Seq + .of("a", "b", "c") + .zipWithIndex(); + } +} diff --git a/libraries/src/test/java/com/baeldung/zip/ZipCollectionTest.java b/libraries/src/test/java/com/baeldung/zip/ZipCollectionTest.java new file mode 100644 index 0000000000..6f6a7b765f --- /dev/null +++ b/libraries/src/test/java/com/baeldung/zip/ZipCollectionTest.java @@ -0,0 +1,85 @@ +package com.baeldung.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 { + + List names; + List ages; + 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); + } + +} \ No newline at end of file