Skip to content

Commit 48cc9a4

Browse files
committed
Removing null checks as suggested by editor for BAEL-3083
1 parent e479bea commit 48cc9a4

File tree

2 files changed

+3
-42
lines changed

2 files changed

+3
-42
lines changed

core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/findastring/FindAStringInGivenList.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
import com.google.common.base.Predicates;
44
import com.google.common.collect.Iterables;
55
import com.google.common.collect.Lists;
6-
import org.apache.commons.collections4.CollectionUtils;
76
import org.apache.commons.collections4.IterableUtils;
87
import org.apache.commons.collections4.IteratorUtils;
9-
import org.apache.commons.collections4.PredicateUtils;
10-
import org.apache.commons.lang3.StringUtils;
118
import java.util.ArrayList;
129
import java.util.List;
1310
import java.util.regex.Pattern;
@@ -38,7 +35,7 @@ public List<String> findUsingLoop(String search, List<String> list) {
3835
List<String> matches = new ArrayList<String>();
3936

4037
for(String str: list) {
41-
if (search != null && str.contains(search)) {
38+
if (str.contains(search)) {
4239
matches.add(str);
4340
}
4441
}
@@ -58,20 +55,15 @@ public List<String> findUsingStream(String search, List<String> list) {
5855
}
5956

6057
public List<String> findUsingGuava(String search, List<String> list) {
61-
if (search == null) {
62-
return Lists.newArrayList();
63-
}
64-
65-
Iterable<String> result = Iterables.filter(Iterables.filter(list,Predicates.notNull()), Predicates.containsPattern(search));
58+
Iterable<String> result = Iterables.filter(list, Predicates.containsPattern(search));
6659

6760
return Lists.newArrayList(result.iterator());
6861
}
6962

7063
public List<String> findUsingCommonsCollection(String search, List<String> list) {
71-
CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
7264
Iterable<String> result = IterableUtils.filteredIterable(list, new org.apache.commons.collections4.Predicate<String>() {
7365
public boolean evaluate(String listElement) {
74-
return search != null && StringUtils.isNotEmpty(listElement) ? listElement.contains(search) : false;
66+
return listElement.contains(search);
7567
}
7668
});
7769

core-java-modules/core-java-collections-list-2/src/test/java/com/baeldung/findastring/FindAStringInListUnitTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class FindAStringInListUnitTest {
1313
list.add("James and Sarah");
1414
list.add("Sam and Louise");
1515
list.add("Jack");
16-
list.add(null);
1716
list.add("");
1817
}
1918

@@ -27,12 +26,6 @@ public void givenAString_whenFoundUsingLoopWithRegex_thenReturnList() {
2726
assertEquals("Jack", matchingElements.get(1));
2827
}
2928

30-
@Test
31-
public void givenAString_whenNullFoundUsingLoopWithRegex_thenReturnEmptyList(){
32-
List matchingElements = findAStringInGivenList.findUsingLoopWithRegex(null, list);
33-
assertEquals(0, matchingElements.size());
34-
}
35-
3629
@Test
3730
public void givenAString_whenFoundUsingLoop_thenReturnList() {
3831
List matchingElements = findAStringInGivenList.findUsingLoop("Jack", list);
@@ -41,12 +34,6 @@ public void givenAString_whenFoundUsingLoop_thenReturnList() {
4134
assertEquals("Jack", matchingElements.get(1));
4235
}
4336

44-
@Test
45-
public void givenAString_whenNullFoundUsingLoop_thenReturnEmptyList(){
46-
List matchingElements = findAStringInGivenList.findUsingLoop(null, list);
47-
assertEquals(0, matchingElements.size());
48-
}
49-
5037

5138
@Test
5239
public void givenAString_whenFoundUsingStream_thenReturnList(){
@@ -56,12 +43,6 @@ public void givenAString_whenFoundUsingStream_thenReturnList(){
5643
assertEquals("Jack", matchingElements.get(1));
5744
}
5845

59-
@Test
60-
public void givenAString_whenNullFoundUsingStream_thenReturnEmptyList(){
61-
List matchingElements = findAStringInGivenList.findUsingStream(null, list);
62-
assertEquals(0, matchingElements.size());
63-
}
64-
6546
@Test
6647
public void givenAString_whenFoundUsingCommonsCollection_thenReturnList(){
6748
List matchingElements = findAStringInGivenList.findUsingCommonsCollection("Jack", list);
@@ -70,12 +51,6 @@ public void givenAString_whenFoundUsingCommonsCollection_thenReturnList(){
7051
assertEquals("Jack", matchingElements.get(1));
7152
}
7253

73-
@Test
74-
public void givenAString_whenNullFoundUsingCommonsCollection_thenReturnEmptyList(){
75-
List matchingElements = findAStringInGivenList.findUsingCommonsCollection(null, list);
76-
assertEquals(0, matchingElements.size());
77-
}
78-
7954
@Test
8055
public void givenAString_whenFoundUsingGuava_thenReturnList(){
8156
List matchingElements = findAStringInGivenList.findUsingGuava("Jack", list);
@@ -84,10 +59,4 @@ public void givenAString_whenFoundUsingGuava_thenReturnList(){
8459
assertEquals("Jack", matchingElements.get(1));
8560
}
8661

87-
@Test
88-
public void givenAString_whenNullFoundUsingGuava_thenReturnEmptyList(){
89-
List matchingElements = findAStringInGivenList.findUsingGuava(null, list);
90-
assertEquals(0, matchingElements.size());
91-
}
92-
9362
}

0 commit comments

Comments
 (0)