Skip to content

Commit 028a784

Browse files
BAEL-5801 - adding iterate list unit test (eugenp#12883)
* BAEL-5801 - adding iterate list unit test * BAEL-5801 - replaced record to class * BAEL-5801 - added spring data dependency * moved source file in java17 module * BAEL-5801 updated pom.xml configurations * BAEL-5801 moved to correct java17 module * updated example * BAEL-5801 - making iterator instance strong typed * BAEL-5801 - removed class example
1 parent 6be9cc1 commit 028a784

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

core-java-modules/core-java-collections-list-4/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
<version>${spring.version}</version>
6363
<scope>test</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.springframework.data</groupId>
67+
<artifactId>spring-data-commons</artifactId>
68+
<version>2.7.5</version>
69+
<scope>test</scope>
70+
</dependency>
6571
</dependencies>
6672

6773
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.list.iteration;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.data.util.StreamUtils;
12+
13+
public class IterateListSimultaneouslyUnitTest {
14+
15+
final List<String> countryName = List.of("USA", "UK", "Germany", "India");
16+
final List<String> countryCode = List.of("+1", "+44", "+49", "+91");
17+
18+
@Test
19+
public void givenTwoLists_whenProcessedByZipping_thenGetJoinedDataFromBothCollections() {
20+
List<String> processedList = StreamUtils.zip(countryName.stream(), countryCode.stream(),
21+
(name, code) -> String.format("%s: %s", name, code))
22+
.collect(Collectors.toList());
23+
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
24+
}
25+
26+
@Test
27+
public void givenTwoLists_whenIterateUsingIterator_thenGetJoinedDataFromBothCollections() {
28+
Iterator<String> nameIterator = countryName.iterator();
29+
Iterator<String> codeIterator = countryCode.iterator();
30+
List<String> processedList = new ArrayList<>();
31+
while (nameIterator.hasNext() && codeIterator.hasNext()) {
32+
String processedData = String.format("%s: %s", nameIterator.next(), codeIterator.next());
33+
processedList.add(processedData);
34+
}
35+
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
36+
}
37+
38+
@Test
39+
public void givenTwoLists_whenIterateUsingLoop_thenGetJoinedDataFromBothCollections() {
40+
List<String> processedList = new ArrayList<>();
41+
for (int i = 0; i < countryName.size(); i++) {
42+
String processedData = String.format("%s: %s", countryName.get(i), countryCode.get(i));
43+
processedList.add(processedData);
44+
}
45+
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)