diff --git a/core-java-modules/core-java-streams-5/pom.xml b/core-java-modules/core-java-streams-5/pom.xml index 042186c9bf..6092b1278e 100644 --- a/core-java-modules/core-java-streams-5/pom.xml +++ b/core-java-modules/core-java-streams-5/pom.xml @@ -48,7 +48,16 @@ protonpack 1.16 - + + org.apache.commons + commons-collections4 + 4.4 + + + one.util + streamex + 0.8.1 + diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java index 3e9cbcdc0b..34e6e62cef 100644 --- a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/streams/firstmatchingelement/FirstMatchingElementUnitTest.java @@ -5,42 +5,35 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import java.util.Optional; import java.util.OptionalInt; import java.util.stream.IntStream; +import org.apache.commons.collections4.IterableUtils; import org.assertj.core.util.Lists; import org.junit.Test; import com.codepoetics.protonpack.StreamUtils; +import com.google.common.collect.Iterables; + +import io.vavr.collection.Stream; +import one.util.streamex.EntryStream; public class FirstMatchingElementUnitTest { - @Test - public void testWithForLoop() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE); - int index = -1; - for (int i = 0; i < dataList.size(); i++) { - if (dataList.get(i) instanceof Boolean) { - index = i; - } - } - assertEquals(2, index); - } + private final List dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0)); @Test - public void testWithIndexOf() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE); - Optional booleanData = dataList.stream() + public void whenCalled_thenFindIndexUsingStream() { + int index = dataList.stream() .filter(data -> data instanceof Boolean) - .findFirst(); - int index = booleanData.isPresent() ? dataList.indexOf(booleanData.get()) : -1; - assertEquals(2, index); + .mapToInt(data -> dataList.indexOf(data)) + .findFirst() + .orElse(-1); + assertEquals(1, index); } @Test - public void testWithIterator() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE); + public void whenCalled_thenFindIndexUsingStreamIterator() { int index = -1; Iterator iterator = dataList.iterator(); while (iterator.hasNext()) { @@ -50,12 +43,11 @@ public class FirstMatchingElementUnitTest { break; } } - assertEquals(2, index); + assertEquals(1, index); } @Test - public void testWithListIterator() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE); + public void whenCalled_thenFindIndexUsingStreamListIterator() { int index = -1; ListIterator listIterator = dataList.listIterator(); while (listIterator.hasNext()) { @@ -64,41 +56,70 @@ public class FirstMatchingElementUnitTest { break; } } - assertEquals(2, index); + assertEquals(1, index); } @Test - public void testWithTakeAWhile() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3); + public void whenCalled_thenFindIndexUsingIntStream() { + int index = IntStream.range(0, dataList.size() - 1) + .filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean) + .findFirst() + .orElse(-1); + assertEquals(1, index); + } + + @Test + public void whenCalled_thenFindIndexUsingStreamTakeWhile() { OptionalInt booleanIndex = dataList.stream() .takeWhile(data -> !(data instanceof Boolean)) .mapToInt(dataList::indexOf) .max(); if (booleanIndex.isPresent() && dataList.get(booleanIndex.getAsInt()) instanceof Boolean) { - assertEquals(2, booleanIndex.getAsInt()); + assertEquals(1, booleanIndex.getAsInt()); } } @Test - public void testWithIntStream() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE); - int index = IntStream.of(0, dataList.size() - 1) - .filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean) - .findFirst() - .orElse(-1); - assertEquals(2, index); - } - - @Test - public void testWithZipWithIndex() { - List dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3); + public void whenCalled_thenFindIndexUsingZipWithIndex() { int index = StreamUtils.zipWithIndex(dataList.stream()) .filter(i -> i.getValue() instanceof Boolean) .mapToInt(i -> Long.valueOf(i.getIndex()) .intValue()) .findFirst() .orElse(-1); - assertEquals(2, index); + assertEquals(1, index); + } + + @Test + public void whenCalled_thenFindIndexUsingGoogleGuava() { + int index = Iterables.indexOf(dataList, data -> data instanceof Boolean); + assertEquals(1, index); + } + + @Test + public void whenCalled_thenFindIndexUsingApacheCommons() { + int index = IterableUtils.indexOf(dataList, data -> data instanceof Boolean); + assertEquals(1, index); + } + + @Test + public void whenCalled_thenFindIndexUsingStreamEx() { + Integer foundIndex = EntryStream.of(dataList) + .filterKeyValue((index, data) -> data instanceof Boolean) + .keys() + .findFirst() + .orElse(-1); + assertEquals(1, foundIndex); + } + + @Test + public void whenCalled_thenFindIndexUsingVavrStream() { + Integer foundIndex = Stream.of(dataList.toArray()) + .zipWithIndex() + .find(tuple -> tuple._1() instanceof Boolean) + .map(tuple -> tuple._2()) + .getOrElse(-1); + assertEquals(1, foundIndex); } }