Skip to content

Commit e08d6d1

Browse files
authored
Merge pull request eugenp#7572 from cscib/BAEL-3083
Example added for BAEL-3083
2 parents c4af872 + c717434 commit e08d6d1

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.findastring;
2+
3+
import com.google.common.base.Predicates;
4+
import com.google.common.collect.Iterables;
5+
import com.google.common.collect.Lists;
6+
import org.apache.commons.collections4.IterableUtils;
7+
import org.apache.commons.collections4.IteratorUtils;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.regex.Pattern;
11+
import java.util.stream.Collectors;
12+
13+
public class FindAStringInGivenList {
14+
15+
16+
public List<String> findUsingLoopWithRegex(String search, List<String> list) {
17+
18+
List<String> matches = new ArrayList<String>();
19+
20+
String pattern = ".*"+search+".*";
21+
Pattern p = Pattern.compile(pattern);
22+
23+
for(String str: list) {
24+
if (p.matcher(str).matches()) {
25+
matches.add(str);
26+
}
27+
}
28+
29+
return matches;
30+
}
31+
32+
33+
public List<String> findUsingLoop(String search, List<String> list) {
34+
35+
List<String> matches = new ArrayList<String>();
36+
37+
for(String str: list) {
38+
if (str.contains(search)) {
39+
matches.add(str);
40+
}
41+
}
42+
43+
return matches;
44+
}
45+
46+
public List<String> findUsingStream(String search, List<String> list) {
47+
48+
List<String> matchingElements =
49+
list.stream()
50+
.filter(str -> str.trim().contains(search))
51+
.collect(Collectors.toList());
52+
53+
return matchingElements;
54+
}
55+
56+
public List<String> findUsingGuava(String search, List<String> list) {
57+
Iterable<String> result = Iterables.filter(list, Predicates.containsPattern(search));
58+
59+
return Lists.newArrayList(result.iterator());
60+
}
61+
62+
public List<String> findUsingCommonsCollection(String search, List<String> list) {
63+
Iterable<String> result = IterableUtils.filteredIterable(list, new org.apache.commons.collections4.Predicate<String>() {
64+
public boolean evaluate(String listElement) {
65+
return listElement.contains(search);
66+
}
67+
});
68+
69+
return IteratorUtils.toList(result.iterator());
70+
}
71+
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.findastring;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import org.junit.Test;
7+
public class FindAStringInListUnitTest {
8+
9+
private static List<String> list = new ArrayList<>();
10+
11+
static {
12+
list.add("Jack and Jill");
13+
list.add("James and Sarah");
14+
list.add("Sam and Louise");
15+
list.add("Jack");
16+
list.add("");
17+
}
18+
19+
private static FindAStringInGivenList findAStringInGivenList = new FindAStringInGivenList();
20+
21+
@Test
22+
public void givenAString_whenFoundUsingLoopWithRegex_thenReturnList() {
23+
List matchingElements = findAStringInGivenList.findUsingLoopWithRegex("Jack", list);
24+
assertEquals(2, matchingElements.size());
25+
assertEquals("Jack and Jill", matchingElements.get(0));
26+
assertEquals("Jack", matchingElements.get(1));
27+
}
28+
29+
@Test
30+
public void givenAString_whenFoundUsingLoop_thenReturnList() {
31+
List matchingElements = findAStringInGivenList.findUsingLoop("Jack", list);
32+
assertEquals(2, matchingElements.size());
33+
assertEquals("Jack and Jill", matchingElements.get(0));
34+
assertEquals("Jack", matchingElements.get(1));
35+
}
36+
37+
38+
@Test
39+
public void givenAString_whenFoundUsingStream_thenReturnList(){
40+
List matchingElements = findAStringInGivenList.findUsingStream("Jack", list);
41+
assertEquals(2, matchingElements.size());
42+
assertEquals("Jack and Jill", matchingElements.get(0));
43+
assertEquals("Jack", matchingElements.get(1));
44+
}
45+
46+
@Test
47+
public void givenAString_whenFoundUsingCommonsCollection_thenReturnList(){
48+
List matchingElements = findAStringInGivenList.findUsingCommonsCollection("Jack", list);
49+
assertEquals(2, matchingElements.size());
50+
assertEquals("Jack and Jill", matchingElements.get(0));
51+
assertEquals("Jack", matchingElements.get(1));
52+
}
53+
54+
@Test
55+
public void givenAString_whenFoundUsingGuava_thenReturnList(){
56+
List matchingElements = findAStringInGivenList.findUsingGuava("Jack", list);
57+
assertEquals(2, matchingElements.size());
58+
assertEquals("Jack and Jill", matchingElements.get(0));
59+
assertEquals("Jack", matchingElements.get(1));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)