Skip to content

Commit 13dfe48

Browse files
Merge pull request #18743 from yabetancourt/BAEL-9387-List-of-String-Contains-Null-or-Empty
BAEL-9387 Check if a List of String contains null or empty
2 parents 690a5af + ff5612c commit 13dfe48

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.listwithnullorempty;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
class ListNullOrEmptyUnitTest {
11+
12+
List<String> list = Arrays.asList("Madrid", null, " ", "Havana", "");
13+
14+
@Test
15+
void givenListWithNullOrEmpty_whenCheckForNullOrEmptyUsingForLoop_thenReturnTrue() {
16+
boolean hasNullOrEmpty = false;
17+
for (String s : list) {
18+
if (s == null || s.isEmpty()) {
19+
hasNullOrEmpty = true;
20+
break;
21+
}
22+
}
23+
24+
assertTrue(hasNullOrEmpty, "List should contain null or empty elements");
25+
}
26+
27+
@Test
28+
void givenListWithNullOrEmpty_whenCheckForNullOrEmptyUsingStreams_thenReturnTrue() {
29+
boolean hasNullOrEmpty = list.stream()
30+
.anyMatch(s -> s == null || s.isEmpty());
31+
32+
assertTrue(hasNullOrEmpty, "List should contain null or blank elements");
33+
}
34+
35+
@Test
36+
void givenListWithNullOrEmpty_whenCheckUsingParallelStream_thenReturnTrue() {
37+
boolean hasNullOrEmpty = list.parallelStream()
38+
.anyMatch(s -> s == null || s.isEmpty());
39+
40+
assertTrue(hasNullOrEmpty, "List should contain null or empty elements");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)