Find Null or Empty Strings in a Java List Example
1. Introduction
Finding Null or empty Strings in a Java List is a common operation because a list can contain null or empty (“”) values. The null or empty can cause bugs or unexpected behavior if not handled properly. In this example, I will demonstrate three approaches to find null or empty string in a list:
- via the List.add method in a
Forloop - via the ArrayList.removeIf method for a mutable list.
- via Stream APIs
filterandcollectmethods.
2. Setup
In this step, I will create a simple Java project in Eclipse with JDK 21 with the following Java source files.
Java Source Files
C:\Users\zzhen\eclipse-workspace\FindFromList\src\org\jcg\zheng\demo>dir
Volume in drive C is OS
Volume Serial Number is FE59-88F4
Directory of C:\Users\zzhen\eclipse-workspace\FindFromList\src\org\jcg\zheng\demo
09/10/2025 09:02 PM .
09/08/2025 07:39 AM ..
09/10/2025 12:17 PM 177 FindNullorEmptyService.java
09/10/2025 09:33 PM 992 ForLoopAddImpl.java
09/10/2025 09:32 PM 926 ListTestData.java
09/10/2025 09:08 PM 988 RemoveIfImpl.java
09/10/2025 09:32 PM 1,028 StreamImmutableImpl.java
09/10/2025 09:33 PM 1,037 StreamMutableImpl.java
6 File(s) 5,148 bytes
2 Dir(s) 886,891,036,672 bytes free
C:\Users\zzhen\eclipse-workspace\FindFromList\src\org\jcg\zheng\demo>3. Test List Data
A list is an ordered, index-based collection that can contain duplicate elements. A List can be both mutable and immutable. In this step, I will create a ListTestData.java that constructs three test data lists:
- fixed-size list – A list created by
Arrays.asListis a fixed-size list. You can replace the content of list, but cannot add nor remove elements. - immutable list – A list is unmodificable. A list created by
List.ofis immutable. - mutable list – A list created by
new ArrayList,new LinkedList, ornew Vectoris mutable.
| List Creation | Mutable/Immutable/Fixed-Size |
| new ArrayList | Mutable |
| new LinkedList | Mutable |
| new Vector | Mutable |
| new Stack | Mutable |
| Stream.collect(Collectors.toList()) | Mutable |
| List.of(…) | Immutable |
| Stream.toList() | Immutable |
| Arrays.asList() | Fixed-size |
ListTestData
package org.jcg.zheng.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListTestData {
public static List<String> dummyMutableList() {
List<String> stringList = new ArrayList<>();
stringList.add(null);
stringList.add("");
stringList.add(" ");
stringList.add("Mary");
return stringList;
}
public static List<String> dummyFixedSizeList() {
return Arrays.asList(null, "", "", "Mary");
}
public static List<String> dummyImmutableList() {
return List.of("Mary");
}
public static void demoFind(FindNullorEmptyService serivce, List<String> inputList) {
List<String> emptyItemList = serivce.findNullOrEmptyItems(inputList);
System.out.println("Null or Empty count: " + emptyItemList.size());
}
public static void main(String[] args) {
System.out.println("can edit the element of fixed_size list");
List<String> dummyFixedSizeList = dummyFixedSizeList();
dummyFixedSizeList.set(0, "z");
List<String> immutableList = dummyImmutableList();
System.out.println("cannot edit the element of immutable list");
immutableList.set(0, null);
}
}- Line 10: The ArrayList is a resizable-array implementation of the
Listinterface. - Line 19: The Arrays.asList() returns a fixed-size list and allows
nullvalue in the list. The list elements can be replaced, but theadd,removeoperations are not allowed. - Line 23: The
List.ofstatic factory methods provide a convenient way to create unmodifiable lists and not allownullvalue in the list. - Line 27: invoke the
findNullOrEmptyItemsmethod to find thenullor empty items. - Line 39: an immutable list throws exception on the
setmethod.
Run the ListTestData as a Java application and capture the output here.
ListTestData Output
can edit the element of fixed_size list cannot edit the element of immutable list Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142) at java.base/java.util.ImmutableCollections$AbstractImmutableList.set(ImmutableCollections.java:262) at org.jcg.zheng.demo.ListTestData.main(ListTestData.java:39)
- Line 3, 6: confirmed that the
UnsupportedOperationExceptionis thrown on thesetmethod for an immutable list.
4. Find Null or Empty From List Interface
In this step, I will create a FindNullorEmptyService.java interface that finds the null or empty from a list of String objects.
FindNullorEmptyService.java
package org.jcg.zheng.demo;
import java.util.List;
public interface FindNullorEmptyService {
public List<String> findNullOrEmptyItems(List<String> stringList);
}
5. Find Null or Empty from List via For Loop
In this step, I will create a ForLoopAddImpl.java that implements the FindNullorEmptyService interface. Note: it loops through the elements and adds null or empty elements to a mutable list if the element is null or isEmpty.
ForLoopAddImpl.java
package org.jcg.zheng.demo;
import java.util.ArrayList;
import java.util.List;
public class ForLoopAddImpl implements FindNullorEmptyService {
@Override
public List<String> findNullOrEmptyItems(final List<String> stringList) {
List<String> nullOrEmptyList = new ArrayList<>();
for (String str : stringList) {
if (str == null || str.isEmpty()) {
nullOrEmptyList.add(str);
}
}
return nullOrEmptyList;
}
public static void main(String[] args) {
FindNullorEmptyService service = new ForLoopAddImpl();
ListTestData.demoFind(service, ListTestData.dummyMutableList());
ListTestData.demoFind(service, ListTestData.dummyFixedSizeList());
ListTestData.demoFind(service, ListTestData.dummyImmutableList());
}
}
- Line 10: the returned list is mutable as it’s created via
new ArrayList(); - Line 11-13:
addsto a mutable list if the element isnullorisEmptyinside theForloop. Note: if the blank space (” “) is considered as empty, then change theisEmptytoisBlank. - Line 23-25: verify the returned list contains
nullor empty.
Run ForLoopAddImpl.java and capture output.
ForLoopAddImpl Output
Null or Empty count: 2 Null or Empty count: 3 Null or Empty count: 0
6. Remove Not Null nor Not Empty via RemoveIf
In this step, I will create a RemoveIfImpl.java that implements the FindNullorEmptyService interface. Note: it removes any element in the list that is not null and not empty. It requires a mutable list argument.
RemoveIfImpl.java
package org.jcg.zheng.demo;
import java.util.List;
public class RemoveIfImpl implements FindNullorEmptyService {
@Override
public List<String> findNullOrEmptyItems(final List<String> stringList) {
stringList.removeIf(str -> str != null && str.isEmpty());
return stringList;
}
public static void main(String[] args) {
FindNullorEmptyService service = new RemoveIfImpl();
ListTestData.demoFind(service, ListTestData.dummyMutableList());
System.out.println("the remove method throws UnsupportedOperationException as List.of is Immutable ");
service.findNullOrEmptyItems(ListTestData.dummyImmutableList());
System.out.println("the remove method throws UnsupportedOperationException as Arrays.asList is Immutable"
+ service.findNullOrEmptyItems(ListTestData.dummyFixedSizeList()));
}
}
- Line 9: the argument list must be mutable.
- Line 16: it works for a mutable list with the
removeIfmethod. - Line 18, 20:
UnsupportedOperationExceptionis thrown for fixed-size or immutable list on theremoveIfmethod.
Run RemoveIfImpl.java and capture output
RemoveIfImpl Output
Null or Empty count: 3 the remove method throws UnsupportedOperationException as List.of is Immutable Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142) at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.removeIf(ImmutableCollections.java:152) at org.jcg.zheng.demo.RemoveIfImpl.findNullOrEmptyItems(RemoveIfImpl.java:9) at org.jcg.zheng.demo.RemoveIfImpl.main(RemoveIfImpl.java:19)
- Line 3,6: confirmed that the
UnsupportedOperationExceptionis thrown on thesetmethod for an immutable list.
7. Find Null or Empty From List via Stream API
7.1 Collect as a Mutable List
In this step, I will create a StreamMutableImpl.java that implements the FindNullorEmptyService interface. The returned list is a mutable list.
StreamMutableImpl.java
package org.jcg.zheng.demo;
import java.util.List;
import java.util.stream.Collectors;
public class StreamMutableImpl implements FindNullorEmptyService {
@Override
public List<String> findNullOrEmptyItems(List<String> stringList) {
return stringList.stream().filter(s -> s == null || s.isEmpty()).collect(Collectors.toList());
}
public static void main(String[] args) {
FindNullorEmptyService service = new StreamMutableImpl();
ListTestData.demoFind(service, ListTestData.dummyMutableList());
ListTestData.demoFind(service, ListTestData.dummyFixedSizeList());
ListTestData.demoFind(service, ListTestData.dummyImmutableList());
}
}
- Line 10: the
Collectors.toList()returns a mutable list.
Run the StreamMutableImpl.java and capture the output:
StreamMutableImpl Output
Null or Empty count: 2 Null or Empty count: 3 Null or Empty count: 0
7.2 Collect as an Immutable List
In this step, I will create a StreamImmutableImpl.java that implements the FindNullorEmptyServic interface. The returned list is an immutable list.
StreamImmutableImpl.java
package org.jcg.zheng.demo;
import java.util.List;
public class StreamImmutableImpl implements FindNullorEmptyService {
@Override
public List<String> findNullOrEmptyItems(List<String> stringList) {
return stringList.stream().filter(s -> s == null || s.isEmpty()).toList();
}
public static void main(String[] args) {
FindNullorEmptyService service = new StreamImmutableImpl();
ListTestData.demoFind(service, ListTestData.dummyMutableList());
ListTestData.demoFind(service, ListTestData.dummyFixedSizeList());
ListTestData.demoFind(service, ListTestData.dummyImmutableList());
System.out.println("the add method throws UnsupportedOperationException as toList is Imutable");
service.findNullOrEmptyItems(ListTestData.dummyFixedSizeList()).add("test");
}
}
- Line 9: the
Stream.toList()returns an immutable list.
Run the StreamImmutableImpl.java and capture the output:
StreamImmutableImpl Output
Null or Empty count: 2 Null or Empty count: 3 Null or Empty count: 0 the add method throws UnsupportedOperationException as toList is Imutable Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142) at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:147) at org.jcg.zheng.demo.StreamImmutableImpl.main(StreamImmutableImpl.java:21)
8. Conclusion
In this example, I created a simple Java program to find null or empty(“”) elements from a list via For Loop, removeIf, and Stream API. The For loop is the simplest and most explicit way, great for readability and beginners. The removeIf works only for a mutable list. The Stream API provides a concise and functional style, especially useful when you want to filter, map, or collect results in a single chain.
9. Download
This was an example of a Java project which found null or empty elements from a list.
You can download the full source code of this example here: Find Null or Empty Strings in a Java List Example

