Java Streams is a powerful API for processing data and performing complex operations in an efficient and elegant manner. However, one of the challenges when working with streams is how to handle null values. In this article, we will explore different ways to handle null values when working with Java streams.

Handling Null values in Stream:

Handling null values can be a bit challenging, especially when dealing with a large dataset. In this article, we will discuss different approaches to handle null values in Java Streams.

Option 1: Using Optional

The easiest way to handle null values in Java Streams is by using the Optional class. Optional is a container object that is designed to handle null values. It provides a convenient way to wrap a value and ensure that it is not null before processing it. The Optional class has several methods that can be used to handle null values, such as isPresent() and orElse().

Here is an example of how to use Optional in Java Streams to handle null values:

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

public class HandlingNull {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Jane", "Jim", null, "Jill");
        names.stream()
                .filter(Objects::nonNull)
                .map(name -> name.toUpperCase())
                .forEach(System.out::println);
    }
}

Output:

JOHN
JANE
JIM
JILL

In the above example, the filter method is used to remove any null values from the list before they are processed. The map method is then used to convert the remaining elements to uppercase.

Option 2: Using filter and orElse:

Another approach to handle null values in Java Streams is by using the filter and orElse methods. The filter method can be used to remove null values from the stream, while the orElse method can be used to provide a default value for any null elements.

Here is an example of how to use the filter and orElse methods in Java Streams to handle null values:

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class HandlingNull {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Jane", "Jim", null, "Jill");
        names.stream()
                .map(name -> Optional.ofNullable(name).orElse("N/A"))
                .map(name -> name.toUpperCase())
                .forEach(System.out::println);
    }
}

Output:

JOHN
JANE
JIM
N/A
JILL

In the above example, the map method is used to wrap each element in an Optional object. The orElse method is then used to provide a default value of “N/A” for any null elements. The map method is then used to convert the remaining elements to uppercase.

Option 3: Using flatMap:

The flatMap method is another way to handle null values in Java Streams. It works by flattening the stream of Optional objects into a stream of values. The flatMap method can be used in conjunction with the Optional class to handle null values.

Here is an example of how to use the flatMap method in Java Streams to handle null values:

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class HandlingNull {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Jane", "Jim", null, "Jill");
        names.stream()
                .map(name -> Optional.ofNullable(name))
                .flatMap(Optional::stream)
                .map(name -> name.toUpperCase())
                .forEach(System.out::println);
    }
}

Output:

JOHN
JANE
JIM
JILL

The flatMap method can be a useful tool for handling null values in Java streams. By using this method, you can easily skip null values and focus on the data that is important to you.

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.