This commit is contained in:
iaforek 2018-03-13 19:03:13 +00:00
commit 96e7c4ed20
2 changed files with 187 additions and 0 deletions

View File

@ -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<T> {
public T findUsingIndexOf(T element, List<T> list) {
int index = list.indexOf(element);
if (index >= 0) {
return element;
}
return null;
}
public boolean findUsingListIterator(T element, List<T> list) {
ListIterator<T> listIterator = list.listIterator();
while (listIterator.hasNext()) {
T elementFromList = listIterator.next();
if (elementFromList.equals(element)) {
return true;
}
}
return false;
}
public boolean findUsingEnhancedForLoop(T element, List<T> list) {
for (T elementFromList : list) {
if (element.equals(elementFromList)) {
return true;
}
}
return false;
}
public T findUsingStream(T element, List<T> list) {
return list.stream()
.filter(integer -> integer.equals(element))
.findFirst()
.orElse(null);
}
public T findUsingParallelStream(T element, List<T> list) {
return list.parallelStream()
.filter(integer -> integer.equals(element))
.findAny()
.orElse(null);
}
public T findUsingGuava(T element, List<T> list) {
T foundElement = Iterables.tryFind(list, new Predicate<T>() {
public boolean apply(T input) {
return element.equals(input);
}
}).orNull();
return foundElement;
}
public T findUsingApacheCommon(T element, List<T> list) {
T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate<T>() {
public boolean evaluate(T input) {
return element.equals(input);
}
});
return foundElement;
}
}

View File

@ -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<Integer> scores = new ArrayList<>();
static {
scores.add(0);
scores.add(1);
scores.add(2);
}
private static FindElementInAList<Integer> 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);
}
}