Core Java

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:

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.asList is 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.of is immutable.
  • mutable list – A list created by new ArrayList, new LinkedList, or new Vector is mutable.
List CreationMutable/Immutable/Fixed-Size
new ArrayListMutable
new LinkedListMutable
new VectorMutable
new StackMutable
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 List interface.
  • Line 19: The Arrays.asList() returns a fixed-size list and allows null value in the list. The list elements can be replaced, but the add, remove operations are not allowed.
  • Line 23: The List.of static factory methods provide a convenient way to create unmodifiable lists and not allow null value in the list.
  • Line 27: invoke the findNullOrEmptyItems method to find the null or empty items.
  • Line 39: an immutable list throws exception on the set method.

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 UnsupportedOperationException is thrown on the set method 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: adds to a mutable list if the element is null or isEmpty inside the For loop. Note: if the blank space (” “) is considered as empty, then change the isEmpty to isBlank.
  • Line 23-25: verify the returned list contains null or 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 removeIf method.
  • Line 18, 20: UnsupportedOperationException is thrown for fixed-size or immutable list on the removeIf method.

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 UnsupportedOperationException is thrown on the set method 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.

Download
You can download the full source code of this example here: Find Null or Empty Strings in a Java List Example

Mary Zheng

Mary graduated from the Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She worked as a lead Software Engineer where she led and worked with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button