BAEL-4959 iterating over the Set (#10864)

This commit is contained in:
mdabrowski-eu 2021-06-06 20:36:01 +02:00 committed by GitHub
parent bfb9a2bcc8
commit d87559a4ba
2 changed files with 80 additions and 0 deletions

View File

@ -44,6 +44,12 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.3</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,74 @@
package com.baeldung.setiteration;
import com.google.common.collect.Sets;
import io.vavr.collection.Stream;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;
class SetIteration {
@Test
void givenSet_whenIteratorUsed_shouldIterateOverElements() {
// given
Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
// when
Iterator<String> namesIterator1 = names.iterator();
Iterator<String> namesIterator2 = names.iterator();
// then
namesIterator1.forEachRemaining(System.out::println);
while(namesIterator2.hasNext()) {
System.out.println(namesIterator2.next());
}
}
@Test
void givenSet_whenStreamUsed_shouldIterateOverElements() {
// given
Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
// when & then
String namesJoined = names.stream()
.map(String::toUpperCase)
.peek(System.out::println)
.collect(Collectors.joining());
}
@Test
void givenSet_whenEnhancedLoopUsed_shouldIterateOverElements() {
// given
Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
// when & then
for (String name : names) {
System.out.println(name);
}
}
@Test
void givenSet_whenMappedToArray_shouldIterateOverElements() {
// given
Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
// when & then
Object[] namesArray = names.toArray();
for (int i = 0; i < namesArray.length; i++) {
System.out.println(i + ": " + namesArray[i]);
}
}
@Test
void givenSet_whenZippedWithIndex_shouldIterateOverElements() {
// given
Set<String> names = Sets.newHashSet("Tom", "Jane", "Karen");
// when & then
Stream.ofAll(names)
.zipWithIndex()
.forEach(t -> System.out.println(t._2() + ": " + t._1()));
}
}