Java 8 – Filter null and empty values from a Stream

In this article, we will discuss how to remove/eliminate null & empty values from a Stream using filter() method

1. Integer Stream – filter null values :

  • A list contains numbers and null values
  • We are trying to remove all null values from Stream using 3 different approaches
  • 1st approach – filter null values using lambda expression filter(num -> null != num)
  • 2nd approach – filter null values using lambda expression filter(num -> Objects.nonNull(num))
  • 3rd approach – filter null values using method references filter(Objects::nonNull)
package net.bench.resources.stream.filter.values;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class StreamFilterNullValuesForInteger {

	public static void main(String[] args) {

		// 1. list of integers
		List<Integer> numbers = Arrays.asList(
				null,1,2,3,null,7,null
				);

		// 1.1 print all number INcluding null
		System.out.println("1. Numbers with null values"
				+ " in Original Stream\n");
		numbers.stream().forEach(System.out::println);

		// 2. filter null values from number stream using Lambda Expression 1
		List<Integer> result1 = numbers
				.stream()
				.filter(num -> null != num)
				.collect(Collectors.toList());

		// 2.1 print all number EXcluding null 
		System.out.println("\n2. Filter null using Lambda Exp -"
				+ " filter(num -> null != num)\n");
		result1.stream().forEach(System.out::println);

		// 3. filter null values from number stream using Lambda Expression 2
		List<Integer> result2 = numbers
				.stream()
				.filter(num -> Objects.nonNull(num))
				.collect(Collectors.toList());

		// 3.1 print all number EXcluding null
		System.out.println("\n3. Filter null using Lambda Exp -"
				+ " filter(num -> Objects.nonNull(num))\n");
		result2.stream().forEach(System.out::println);

		// 4. filter null values from number stream using Method References
		List<Integer> result3 = numbers
				.stream()
				.filter(Objects::nonNull)
				.collect(Collectors.toList());

		// 4.1 print all number EXcluding null
		System.out.println("\n4. Filter null using Method Reference -"
				+ " filter(Objects::nonNull)\n");
		result3.stream().forEach(System.out::println);
	}
}

Output:

1. Numbers with null values in Original Stream

null
1
2
3
null
7
null

2. Filter null using Lambda Exp - filter(num -> null != num)

1
2
3
7

3. Filter null using Lambda Exp - filter(num -> Objects.nonNull(num))

1
2
3
7

4. Filter null using Method Reference - filter(Objects::nonNull)

1
2
3
7

2. Stream of Strings – filter null values :

  • A list contains Strings elements and null values
  • Here also, we are trying to remove all null values present in the Stream of String using 3 different approaches
  • 1st approach – filter null values using lambda expression filter(str -> null != str)
  • 2nd approach – filter null values using lambda expression filter(str -> Objects.nonNull(str))
  • 3rd approach – filter null values using method references filter(Objects::nonNull)
package net.bench.resources.stream.filter.values;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class StreamFilterNullValuesForString {

	public static void main(String[] args) {

		// 1. list of Strings
		List<String> techStack = Arrays.asList(
				null,
				"Java",
				"Spring",
				"Hibernate",
				null,
				"Python", 
				null
				);

		// 1.1 print all String INcluding null
		System.out.println("1. Strings in Original Stream\n");
		techStack.stream().forEach(System.out::println);

		// 2. filter null values from stream using Lambda Expression 1
		List<String> result1 = techStack
				.stream()
				.filter(str -> null != str)
				.collect(Collectors.toList());

		// 2.1 print all String EXcluding null 
		System.out.println("\n2. Filter null using Lambda Exp -"
				+ " filter(num -> null != num)\n");
		result1.stream().forEach(System.out::println);

		// 3. filter null values from stream using Lambda Expression 2
		List<String> result2 = techStack
				.stream()
				.filter(str -> Objects.nonNull(str))
				.collect(Collectors.toList());

		// 3.1 print all String EXcluding null
		System.out.println("\n3. Filter null using Lambda Exp -"
				+ " filter(num -> Objects.nonNull(num))\n");
		result2.stream().forEach(System.out::println);

		// 4. filter null values from stream using Method References
		List<String> result3 = techStack
				.stream()
				.filter(Objects::nonNull)
				.collect(Collectors.toList());

		// 4.1 print all String EXcluding null
		System.out.println("\n4. Filter null using Method Reference -"
				+ " filter(Objects::nonNull)\n");
		result3.stream().forEach(System.out::println);
	}
}

Output:

1. Strings in Original Stream

null
Java
Spring
Hibernate
null
Python
null

2. Filter null using Lambda Exp - filter(num -> null != num)

Java
Spring
Hibernate
Python

3. Filter null using Lambda Exp - filter(num -> Objects.nonNull(num))

Java
Spring
Hibernate
Python

4. Filter null using Method Reference - filter(Objects::nonNull)

Java
Spring
Hibernate
Python

3. Stream of Strings – filter empty values :

  • A list contains Strings with empty values or String with whitespaces
  • In this String list, we have 3 Strings with 0 whitespace, 1 whitespace and 2 whitespaces
  • We are trying to eliminate/remove empty or String with whitespaces from the list using filter() method
  • So to achieve this, we are filtering using lambda expression filter(str -> null != str && str.trim().length() > 0)
package net.bench.resources.stream.filter.values;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamFilterEmptyValues {

	public static void main(String[] args) {

		// 1. list of Strings
		List<String> techStack = Arrays.asList(
				"Java",
				"  ", // 2 white-spaces
				"Spring",
				"", // 0 white-space
				"Hibernate",
				" ", // 1 white-space
				"Python"
				);

		// 1.1 print all String INcluding null
		System.out.println("1. Strings in Original Stream\n"
				+ techStack);

		// 2. filter empty values after trimming
		List<String> result1 = techStack
				.stream()
				.filter(str -> null != str && str.trim().length() > 0)
				.collect(Collectors.toList());

		// 2.1 print all number EXcluding null 
		System.out.println("\n2. Filter empty values or white-spaces\n");
		result1.stream().forEach(System.out::println);
	}
}

Output:

1. Strings in Original Stream
[Java,   , Spring, , Hibernate,  , Python]

2. Filter empty values or white-spaces

Java
Spring
Hibernate
Python

References:

Happy Coding !!
Happy Learning !!

Java 8 - Filter a Map by its Key and Value
Java 8 - Stream findFirst() v/s findAny() methods