Skip to content

Commit 3ad0cb6

Browse files
ankishagarwalpivovarit
authored andcommitted
Request for BAEL-1584 (eugenp#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
1 parent 6fdfc3c commit 3ad0cb6

2 files changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.findanelement;
2+
3+
import java.util.List;
4+
import java.util.ListIterator;
5+
import org.apache.commons.collections4.IterableUtils;
6+
import com.google.common.base.Predicate;
7+
import com.google.common.collect.Iterables;
8+
9+
public class FindElementInAList<T> {
10+
11+
public T findUsingIndexOf(T element, List<T> list) {
12+
int index = list.indexOf(element);
13+
if (index >= 0) {
14+
return element;
15+
}
16+
return null;
17+
}
18+
19+
public boolean findUsingListIterator(T element, List<T> list) {
20+
ListIterator<T> listIterator = list.listIterator();
21+
while (listIterator.hasNext()) {
22+
T elementFromList = listIterator.next();
23+
if (elementFromList.equals(element)) {
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
public boolean findUsingEnhancedForLoop(T element, List<T> list) {
31+
for (T elementFromList : list) {
32+
if (element.equals(elementFromList)) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
39+
public T findUsingStream(T element, List<T> list) {
40+
return list.stream()
41+
.filter(integer -> integer.equals(element))
42+
.findFirst()
43+
.orElse(null);
44+
}
45+
46+
public T findUsingParallelStream(T element, List<T> list) {
47+
return list.parallelStream()
48+
.filter(integer -> integer.equals(element))
49+
.findAny()
50+
.orElse(null);
51+
}
52+
53+
public T findUsingGuava(T element, List<T> list) {
54+
T foundElement = Iterables.tryFind(list, new Predicate<T>() {
55+
public boolean apply(T input) {
56+
return element.equals(input);
57+
}
58+
}).orNull();
59+
return foundElement;
60+
}
61+
62+
public T findUsingApacheCommon(T element, List<T> list) {
63+
T foundElement = IterableUtils.find(list, new org.apache.commons.collections4.Predicate<T>() {
64+
public boolean evaluate(T input) {
65+
return element.equals(input);
66+
}
67+
});
68+
return foundElement;
69+
}
70+
71+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.baeldung.findanelement;
2+
3+
import static org.junit.Assert.assertNull;
4+
import static org.junit.Assert.assertTrue;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import org.junit.Test;
8+
9+
public class FindAnElementTest {
10+
11+
private static List<Integer> scores = new ArrayList<>();
12+
static {
13+
scores.add(0);
14+
scores.add(1);
15+
scores.add(2);
16+
}
17+
18+
private static FindElementInAList<Integer> findElementInAList = new FindElementInAList<>();
19+
20+
@Test
21+
public void givenElement_whenFoundUsingIndexOf_thenReturnElement() {
22+
Integer scoreToFind = 1;
23+
Integer score = findElementInAList.findUsingIndexOf(scoreToFind, scores);
24+
assertTrue(score.equals(scoreToFind));
25+
}
26+
27+
@Test
28+
public void givenElement_whenNotFoundUsingListIterator_thenReturnNull() {
29+
boolean found = findElementInAList.findUsingListIterator(5, scores);
30+
assertTrue(!found);
31+
}
32+
33+
@Test
34+
public void givenElement_whenFoundListIterator_thenReturnElement() {
35+
Integer scoreToFind = 1;
36+
boolean found = findElementInAList.findUsingListIterator(scoreToFind, scores);
37+
assertTrue(found);
38+
}
39+
40+
@Test
41+
public void givenElement_whenNotFoundUsingIndexOf_thenReturnNull() {
42+
Integer score = findElementInAList.findUsingIndexOf(5, scores);
43+
assertNull(score);
44+
}
45+
46+
@Test
47+
public void givenElement_whenFoundUsingEnhancedForLoop_thenReturnElement() {
48+
Integer scoreToFind = 1;
49+
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
50+
assertTrue(found);
51+
}
52+
53+
@Test
54+
public void givenElement_whenNotFoundUsingEnhancedForLoop_thenReturnNull() {
55+
Integer scoreToFind = 5;
56+
boolean found = findElementInAList.findUsingEnhancedForLoop(scoreToFind, scores);
57+
assertTrue(!found);
58+
}
59+
60+
@Test
61+
public void givenElement_whenFoundUsingStream_thenReturnElement() {
62+
Integer scoreToFind = 1;
63+
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
64+
assertTrue(score.equals(scoreToFind));
65+
}
66+
67+
@Test
68+
public void givenElement_whenNotFoundUsingStream_thenReturnNull() {
69+
Integer scoreToFind = 5;
70+
Integer score = findElementInAList.findUsingStream(scoreToFind, scores);
71+
assertNull(score);
72+
}
73+
74+
@Test
75+
public void givenElement_whenFoundUsingParallelStream_thenReturnElement() {
76+
Integer scoreToFind = 1;
77+
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
78+
assertTrue(score.equals(scoreToFind));
79+
}
80+
81+
@Test
82+
public void givenElement_whenNotFoundUsingParallelStream_thenReturnNull() {
83+
Integer scoreToFind = 5;
84+
Integer score = findElementInAList.findUsingParallelStream(scoreToFind, scores);
85+
assertNull(score);
86+
}
87+
88+
@Test
89+
public void givenElement_whenFoundUsingGuava_thenReturnElement() {
90+
Integer scoreToFind = 1;
91+
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
92+
assertTrue(score.equals(scoreToFind));
93+
}
94+
95+
@Test
96+
public void givenElement_whenNotFoundUsingGuava_thenReturnNull() {
97+
Integer scoreToFind = 5;
98+
Integer score = findElementInAList.findUsingGuava(scoreToFind, scores);
99+
assertNull(score);
100+
}
101+
102+
@Test
103+
public void givenElement_whenFoundUsingApacheCommons_thenReturnElement() {
104+
Integer scoreToFind = 1;
105+
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
106+
assertTrue(score.equals(scoreToFind));
107+
}
108+
109+
@Test
110+
public void givenElement_whenNotFoundUsingApacheCommons_thenReturnNull() {
111+
Integer scoreToFind = 5;
112+
Integer score = findElementInAList.findUsingApacheCommon(scoreToFind, scores);
113+
assertNull(score);
114+
}
115+
116+
}

0 commit comments

Comments
 (0)