From 3ad0cb693cf0212f8f0b6f761c25b2ae3567fd44 Mon Sep 17 00:00:00 2001 From: ankishagarwal <34980371+ankishagarwal@users.noreply.github.com> Date: Wed, 14 Mar 2018 00:18:11 +0530 Subject: [PATCH] Request for BAEL-1584 (#3740) * Added for BAEL-1584 * updated for BAEL-1584 * Updated as per Suggestions from Josh, for BAEL-1584 * Removed wrapper class and updated few methods to return boolean instead of Integer --- .../findanelement/FindElementInAList.java | 71 +++++++++++ .../findanelement/FindAnElementTest.java | 116 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java create mode 100644 core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java diff --git a/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java b/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java new file mode 100644 index 0000000000..2f402ee72b --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/findanelement/FindElementInAList.java @@ -0,0 +1,71 @@ +package com.baeldung.findanelement; + +import java.util.List; +import java.util.ListIterator; +import org.apache.commons.collections4.IterableUtils; +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; + +public class FindElementInAList { + + public T findUsingIndexOf(T element, List list) { + int index = list.indexOf(element); + if (index >= 0) { + return element; + } + return null; + } + + public boolean findUsingListIterator(T element, List list) { + ListIterator listIterator = list.listIterator(); + while (listIterator.hasNext()) { + T elementFromList = listIterator.next(); + if (elementFromList.equals(element)) { + return true; + } + } + return false; + } + + public boolean findUsingEnhancedForLoop(T element, List list) { + for (T elementFromList : list) { + if (element.equals(elementFromList)) { + return true; + } + } + return false; + } + + public T findUsingStream(T element, List list) { + return list.stream() + .filter(integer -> integer.equals(element)) + .findFirst() + .orElse(null); + } + + public T findUsingParallelStream(T element, List list) { + return list.parallelStream() + .filter(integer -> integer.equals(element)) + .findAny() + .orElse(null); + } + + public T findUsingGuava(T element, List list) { + T foundElement = Iterables.tryFind(list, new Predicate() { + public boolean apply(T input) { + return element.equals(input); + } + }).orNull(); + return foundElement; + } + + public T findUsingApacheCommon(T element, List list) { + T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate() { + public boolean evaluate(T input) { + return element.equals(input); + } + }); + return foundElement; + } + +} \ No newline at end of file diff --git a/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java b/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java new file mode 100644 index 0000000000..1fef2d98e7 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/findanelement/FindAnElementTest.java @@ -0,0 +1,116 @@ +package com.baeldung.findanelement; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; + +public class FindAnElementTest { + + private static List scores = new ArrayList<>(); + static { + scores.add(0); + scores.add(1); + scores.add(2); + } + + private static FindElementInAList findElementInAList = new FindElementInAList<>(); + + @Test + public void givenElement_whenFoundUsingIndexOf_thenReturnElement() { + Integer scoreToFind = 1; + Integer score = findElementInAList.findUsingIndexOf(scoreToFind, scores); + assertTrue(score.equals(scoreToFind)); + } + + @Test + public void givenElement_whenNotFoundUsingListIterator_thenReturnNull() { + boolean found = findElementInAList.findUsingListIterator(5, scores); + assertTrue(!found); + } + + @Test + public void givenElement_whenFoundListIterator_thenReturnElement() { + Integer scoreToFind = 1; + boolean found = findElementInAList.findUsingListIterator(scoreToFind, scores); + assertTrue(found); + } + + @Test + public void givenElement_whenNotFoundUsingIndexOf_thenReturnNull() { + Integer score = findElementInAList.findUsingIndexOf(5, scores); + assertNull(score); + } + + @Test + public void givenElement_whenFoundUsingEnhancedForLoop_thenReturnElement() { + Integer scoreToFind = 1; + boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores); + assertTrue(found); + } + + @Test + public void givenElement_whenNotFoundUsingEnhancedForLoop_thenReturnNull() { + Integer scoreToFind = 5; + boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores); + assertTrue(!found); + } + + @Test + public void givenElement_whenFoundUsingStream_thenReturnElement() { + Integer scoreToFind = 1; + Integer score = findElementInAList.findUsingStream(scoreToFind, scores); + assertTrue(score.equals(scoreToFind)); + } + + @Test + public void givenElement_whenNotFoundUsingStream_thenReturnNull() { + Integer scoreToFind = 5; + Integer score = findElementInAList.findUsingStream(scoreToFind, scores); + assertNull(score); + } + + @Test + public void givenElement_whenFoundUsingParallelStream_thenReturnElement() { + Integer scoreToFind = 1; + Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores); + assertTrue(score.equals(scoreToFind)); + } + + @Test + public void givenElement_whenNotFoundUsingParallelStream_thenReturnNull() { + Integer scoreToFind = 5; + Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores); + assertNull(score); + } + + @Test + public void givenElement_whenFoundUsingGuava_thenReturnElement() { + Integer scoreToFind = 1; + Integer score = findElementInAList.findUsingGuava(scoreToFind, scores); + assertTrue(score.equals(scoreToFind)); + } + + @Test + public void givenElement_whenNotFoundUsingGuava_thenReturnNull() { + Integer scoreToFind = 5; + Integer score = findElementInAList.findUsingGuava(scoreToFind, scores); + assertNull(score); + } + + @Test + public void givenElement_whenFoundUsingApacheCommons_thenReturnElement() { + Integer scoreToFind = 1; + Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores); + assertTrue(score.equals(scoreToFind)); + } + + @Test + public void givenElement_whenNotFoundUsingApacheCommons_thenReturnNull() { + Integer scoreToFind = 5; + Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores); + assertNull(score); + } + +} \ No newline at end of file