Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}