BAEL-5801 - adding iterate list unit test (#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
This commit is contained in:
Kumar Prabhash Anand 2022-11-16 19:14:19 +01:00 committed by GitHub
parent 1c3569f2f3
commit 748ccaef61
2 changed files with 54 additions and 0 deletions

View File

@ -62,6 +62,12 @@
<version>${spring.version}</version> <version>${spring.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.7.5</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,48 @@
package com.baeldung.list.iteration;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.springframework.data.util.StreamUtils;
public class IterateListSimultaneouslyUnitTest {
final List<String> countryName = List.of("USA", "UK", "Germany", "India");
final List<String> countryCode = List.of("+1", "+44", "+49", "+91");
@Test
public void givenTwoLists_whenProcessedByZipping_thenGetJoinedDataFromBothCollections() {
List<String> processedList = StreamUtils.zip(countryName.stream(), countryCode.stream(),
(name, code) -> String.format("%s: %s", name, code))
.collect(Collectors.toList());
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
}
@Test
public void givenTwoLists_whenIterateUsingIterator_thenGetJoinedDataFromBothCollections() {
Iterator<String> nameIterator = countryName.iterator();
Iterator<String> codeIterator = countryCode.iterator();
List<String> processedList = new ArrayList<>();
while (nameIterator.hasNext() && codeIterator.hasNext()) {
String processedData = String.format("%s: %s", nameIterator.next(), codeIterator.next());
processedList.add(processedData);
}
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
}
@Test
public void givenTwoLists_whenIterateUsingLoop_thenGetJoinedDataFromBothCollections() {
List<String> processedList = new ArrayList<>();
for (int i = 0; i < countryName.size(); i++) {
String processedData = String.format("%s: %s", countryName.get(i), countryCode.get(i));
processedList.add(processedData);
}
assertThat(processedList).containsExactly("USA: +1", "UK: +44", "Germany: +49", "India: +91");
}
}