Example added for BAEL-3083
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
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.CollectionUtils;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.apache.commons.collections4.IteratorUtils;
|
||||
import org.apache.commons.collections4.PredicateUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FindAStringInGivenList {
|
||||
|
||||
|
||||
public List findUsingLoopWithRegex(String search, List<String> list) {
|
||||
|
||||
List<String> matches = new ArrayList<String>();
|
||||
|
||||
String pattern = ".*\\b"+search+"\\b.*";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
|
||||
for(String str: list) {
|
||||
if (p.matcher(str).matches()) {
|
||||
matches.add(str);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
|
||||
public List findUsingLoop(String search, List<String> list) {
|
||||
|
||||
List<String> matches = new ArrayList<String>();
|
||||
|
||||
for(String str: list) {
|
||||
if (search != null && str.contains(search)) {
|
||||
matches.add(str);
|
||||
}
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
public List findUsingStream(String search, List<String> list) {
|
||||
|
||||
List<String> matchingElements =
|
||||
list.stream()
|
||||
.filter(str -> search != null)
|
||||
.filter(str -> str.trim().contains(search))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return matchingElements;
|
||||
}
|
||||
|
||||
public List<String> findUsingGuava(String search, List<String> list) {
|
||||
if (search == null) return Lists.newArrayList();
|
||||
|
||||
Iterable<String> result = Iterables.filter(Iterables.filter(list,Predicates.notNull()), Predicates.containsPattern(search));
|
||||
|
||||
return Lists.newArrayList(result.iterator());
|
||||
}
|
||||
|
||||
public List<String> findUsingApacheCommon(String search, List<String> list) {
|
||||
CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
|
||||
Iterable<String> result = IterableUtils.filteredIterable(list, new org.apache.commons.collections4.Predicate<String>() {
|
||||
public boolean evaluate(String listElement) {
|
||||
return search != null && StringUtils.isNotEmpty(listElement) ? listElement.contains(search) : false;
|
||||
}
|
||||
});
|
||||
|
||||
return IteratorUtils.toList(result.iterator());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
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(null);
|
||||
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_whenNullFoundUsingLoopWithRegex_thenReturnEmptyList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingLoopWithRegex(null, list);
|
||||
assertEquals(0, matchingElements.size());
|
||||
}
|
||||
|
||||
@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_whenNullFoundUsingLoop_thenReturnEmptyList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingLoop(null, list);
|
||||
assertEquals(0, matchingElements.size());
|
||||
}
|
||||
|
||||
|
||||
@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_whenNullFoundUsingStream_thenReturnEmptyList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingStream(null, list);
|
||||
assertEquals(0, matchingElements.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenFoundUsingApacheCommons_thenReturnList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingApacheCommon("Jack", list);
|
||||
assertEquals(2, matchingElements.size());
|
||||
assertEquals("Jack and Jill", matchingElements.get(0));
|
||||
assertEquals("Jack", matchingElements.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenNullFoundUsingApacheCommons_thenReturnEmptyList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingApacheCommon(null, list);
|
||||
assertEquals(0, matchingElements.size());
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAString_whenNullFoundUsingGuava_thenReturnEmptyList(){
|
||||
List matchingElements = findAStringInGivenList.findUsingGuava(null, list);
|
||||
assertEquals(0, matchingElements.size());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user