diff --git a/core-java-modules/core-java-streams-5/pom.xml b/core-java-modules/core-java-streams-5/pom.xml index adffb4cf19..bdfa4cb76d 100644 --- a/core-java-modules/core-java-streams-5/pom.xml +++ b/core-java-modules/core-java-streams-5/pom.xml @@ -48,21 +48,11 @@ guava ${guava.version} - - com.codepoetics - 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 34e6e62cef..a7bcef55ec 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,19 +5,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -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 { private final List dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0)); @@ -70,25 +65,18 @@ public class FirstMatchingElementUnitTest { @Test public void whenCalled_thenFindIndexUsingStreamTakeWhile() { - OptionalInt booleanIndex = dataList.stream() + int lastIndex = dataList.size() - 1; + int predicateIndex = dataList.stream() .takeWhile(data -> !(data instanceof Boolean)) .mapToInt(dataList::indexOf) - .max(); - - if (booleanIndex.isPresent() && dataList.get(booleanIndex.getAsInt()) instanceof Boolean) { - assertEquals(1, booleanIndex.getAsInt()); - } - } - - @Test - public void whenCalled_thenFindIndexUsingZipWithIndex() { - int index = StreamUtils.zipWithIndex(dataList.stream()) - .filter(i -> i.getValue() instanceof Boolean) - .mapToInt(i -> Long.valueOf(i.getIndex()) - .intValue()) - .findFirst() + .max() .orElse(-1); - assertEquals(1, index); + + if (predicateIndex != 1 && predicateIndex != lastIndex) { + assertEquals(1, predicateIndex + 1); + } else { + assertEquals(lastIndex, predicateIndex); + } } @Test @@ -99,27 +87,8 @@ public class FirstMatchingElementUnitTest { @Test public void whenCalled_thenFindIndexUsingApacheCommons() { + List dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0)); 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); - } }