Merge pull request #7572 from cscib/BAEL-3083
Example added for BAEL-3083
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.findastring;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.apache.commons.collections4.IteratorUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FindAStringInGivenList {
|
||||
|
||||
|
||||
public List<String> findUsingLoopWithRegex(String search, List<String> list) {
|
||||
|
||||
List<String> matches = new ArrayList<String>();
|
||||
|
||||
String pattern = ".*"+search+".*";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
|
||||
for(String str: list) {
|
||||
if (p.matcher(str).matches()) {
|
||||
matches.add(str);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
|
||||
public List<String> findUsingLoop(String search, List<String> list) {
|
||||
|
||||
List<String> matches = new ArrayList<String>();
|
||||
|
||||
for(String str: list) {
|
||||
if (str.contains(search)) {
|
||||
matches.add(str);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
public List<String> findUsingStream(String search, List<String> list) {
|
||||
|
||||
List<String> matchingElements =
|
||||
list.stream()
|
||||
.filter(str -> str.trim().contains(search))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return matchingElements;
|
||||
}
|
||||
|
||||
public List<String> findUsingGuava(String search, List<String> list) {
|
||||
Iterable<String> result = Iterables.filter(list, Predicates.containsPattern(search));
|
||||
|
||||
return Lists.newArrayList(result.iterator());
|
||||
}
|
||||
|
||||
public List<String> findUsingCommonsCollection(String search, List<String> list) {
|
||||
Iterable<String> result = IterableUtils.filteredIterable(list, new org.apache.commons.collections4.Predicate<String>() {
|
||||
public boolean evaluate(String listElement) {
|
||||
return listElement.contains(search);
|
||||
}
|
||||
});
|
||||
|
||||
return IteratorUtils.toList(result.iterator());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.findastring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.Test;
|
||||
public class FindAStringInListUnitTest {
|
||||
|
||||
private static List<String> list = new ArrayList<>();
|
||||
|
||||
static {
|
||||
list.add("Jack and Jill");
|
||||
list.add("James and Sarah");
|
||||
list.add("Sam and Louise");
|
||||
list.add("Jack");
|
||||
list.add("");
|
||||
}
|
||||
|
||||
private static FindAStringInGivenList findAStringInGivenList = new FindAStringInGivenList();
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingLoopWithRegex_thenReturnList() {
|
||||
List matchingElements = findAStringInGivenList.findUsingLoopWithRegex("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingLoop_thenReturnList() {
|
||||
List matchingElements = findAStringInGivenList.findUsingLoop("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingStream_thenReturnList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingStream("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingCommonsCollection_thenReturnList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingCommonsCollection("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingGuava_thenReturnList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingGuava("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user