Skip to content

Commit 1957750

Browse files
daveRanjanpivovarit
authored andcommitted
How to zip two collections in Java? (eugenp#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
1 parent 57279a1 commit 1957750

4 files changed

Lines changed: 132 additions & 1 deletion

File tree

core-java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,4 @@
417417
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
418418

419419
</properties>
420-
</project>
420+
</project>

libraries/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@
349349
<artifactId>java-lsh</artifactId>
350350
<version>${java-lsh.version}</version>
351351
</dependency>
352+
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
353+
<dependency>
354+
<groupId>com.google.guava</groupId>
355+
<artifactId>guava</artifactId>
356+
<version>21.0</version>
357+
</dependency>
352358
<dependency>
353359
<groupId>au.com.dius</groupId>
354360
<artifactId>pact-jvm-consumer-junit_2.11</artifactId>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.zip;
2+
3+
import com.google.common.collect.Streams;
4+
import org.jooq.lambda.Seq;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.IntStream;
9+
10+
public class ZipCollectionExample {
11+
static List<String> names = Arrays.asList("John", "Jane", "Jack", "Dennis");
12+
13+
static List<Integer> ages = Arrays.asList(24, 25, 27);
14+
15+
public static void main(String[] args) {
16+
// Using Streams API from Guava 21
17+
Streams
18+
.zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age)
19+
.forEach(System.out::println);
20+
21+
// Using native Java 8 Int Stream
22+
IntStream
23+
.range(0, Math.min(names.size(), ages.size()))
24+
.mapToObj(i -> names.get(i) + ":" + ages.get(i))
25+
.forEach(System.out::println);
26+
27+
// Using jOOL
28+
Seq
29+
.of("John", "Jane", "Dennis")
30+
.zip(Seq.of(24, 25, 27));
31+
32+
Seq
33+
.of("John", "Jane", "Dennis")
34+
.zip(Seq.of(24, 25, 27), (x, y) -> x + ":" + y);
35+
36+
Seq
37+
.of("a", "b", "c")
38+
.zipWithIndex();
39+
}
40+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.zip;
2+
3+
import com.google.common.collect.Streams;
4+
import org.jooq.lambda.Seq;
5+
import org.jooq.lambda.tuple.Tuple2;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.IntStream;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class ZipCollectionTest {
17+
18+
List<String> names;
19+
List<Integer> ages;
20+
List<String> expectedOutput;
21+
22+
@Before
23+
public void setUp() throws Exception {
24+
names = Arrays.asList("John", "Jane", "Jack", "Dennis");
25+
ages = Arrays.asList(24, 25, 27);
26+
expectedOutput = Arrays.asList("John:24", "Jane:25", "Jack:27");
27+
}
28+
29+
@Test
30+
public void zipCollectionUsingGuava21() {
31+
List<String> output = Streams
32+
.zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age)
33+
.collect(Collectors.toList());
34+
35+
assertEquals(output, expectedOutput);
36+
}
37+
38+
@Test
39+
public void zipCollectionUsingIntStream() {
40+
List<String> output = IntStream
41+
.range(0, Math.min(names.size(), ages.size()))
42+
.mapToObj(i -> names.get(i) + ":" + ages.get(i))
43+
.collect(Collectors.toList());
44+
45+
assertEquals(output, expectedOutput);
46+
}
47+
48+
@Test
49+
public void zipCollectionUsingJool() {
50+
Seq<String> output = Seq
51+
.of("John", "Jane", "Jack")
52+
.zip(Seq.of(24, 25, 27), (x, y) -> x + ":" + y);
53+
54+
assertEquals(output.toList(), expectedOutput);
55+
}
56+
57+
@Test
58+
public void zipCollectionUsingJoolTuple() {
59+
Seq<Tuple2<String, Integer>> output = Seq
60+
.of("John", "Jane", "Dennis")
61+
.zip(Seq.of(24, 25, 27));
62+
63+
Tuple2<String, Integer> element1 = new Tuple2<String, Integer>("John", 24);
64+
Tuple2<String, Integer> element2 = new Tuple2<String, Integer>("Jane", 25);
65+
Tuple2<String, Integer> element3 = new Tuple2<String, Integer>("Dennis", 27);
66+
67+
List<Tuple2> expectedOutput = Arrays.asList(element1, element2, element3);
68+
assertEquals(output.collect(Collectors.toList()), expectedOutput);
69+
}
70+
71+
@Test
72+
public void zipCollectionUsingJoolWithIndex() {
73+
Seq<Tuple2<String, Long>> output = Seq
74+
.of("John", "Jane", "Dennis")
75+
.zipWithIndex();
76+
77+
Tuple2<String, Long> element1 = new Tuple2<>("John", 0L);
78+
Tuple2<String, Long> element2 = new Tuple2<>("Jane", 1L);
79+
Tuple2<String, Long> element3 = new Tuple2<>("Dennis", 2L);
80+
81+
List<Tuple2> expectedOutput = Arrays.asList(element1, element2, element3);
82+
assertEquals(output.collect(Collectors.toList()), expectedOutput);
83+
}
84+
85+
}

0 commit comments

Comments
 (0)