[JAVA] Convert an array that may be null to a stream

I'm a little worried every time, so I'll write it as a memorandum

Summary

All you have to do is remember the idea of returning an empty stream with orElse

Summary


Optional.ofNullable(arr)
  .map(Arrays::stream).orElse(Stream.empty())
  .forEach(System.out::println);

Details

Thing you want to do

I want to stream an array that may be null.

problem

Extended for statements, ╩╗Arrays # stream, and Stream # of` are not null safe. But it is troublesome to check null every time.

When using a for statement


String[] arr = null;

for (String s : arr) { //Here NullPointerException
  System.out.println(s);
}

When using Arrays


String[] arr = null;

Arrays.stream(arr) //Here NullPointerException
  .forEach(System.out::println);

// Stream#of also internally Arrays#Seems to be using stream
Stream.of(arr) //Here NullPointerException
  .forEach(System.out::println);

Use Optional

When using Optional


String[] arr = null;

Optional.ofNullable(arr)     //Optional at this point<String[]>
     .map(Arrays.stream)     //Optional at this point<Stream<String>>
     .orElse(Stream.empty()) //Stream at this point<String>,Stream the contents of the array
     .forEach(System.out::println);

Recommended Posts

Convert an array that may be null to a stream
[Ruby] I want to put an array in a variable. I want to convert to an array
How to convert an array of Strings to an array of objects with the Stream API
Convert a string to a character-by-character array with swift
[Java] How to convert one element of a String type array to an Int type
How to use an array for a TreeMap key
How to convert a file to a byte array in Java
Null value is entered when assigning to an array
[Java] I want to convert a byte array to a hexadecimal number
Convert a Java byte array to a string in hexadecimal notation
How to change a string in an array to a number in Ruby
I want to write a loop that references an index with Java 8's Stream API
Find a Switch statement that can be converted to a Switch expression
[Java] Convert ArrayList to array
Convert 2D array to csv format with Java 8 Stream API
[Ruby] How to batch convert strings in an array to numbers
I want to convert an array to Active Record Relation with Rails
Cast an array of Strings to a List of Integers in Java
Want to throw an IOException out of Stream? in that case
When you receive a call, send an SMS to that number
Convert alphabet to 26 base + array length
How to make a Java array
[Small story] Convert Stream to Iterable
[Java] Convert array to ArrayList * Caution
To not be a static uncle
Convert Swift 2D array to C 2D array
A program that determines whether the entered integer is close to an integer
[Java] How to turn a two-dimensional array with an extended for statement
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
I want to ForEach an array with a Lambda expression in Java
There seems to be a word "de-Java".
How to add a new hash / array
A program that calculates factorials from 2 to 100
[Java] Convert Object type null to String type
Why null is said to be bad
Corresponds to a property whose type is an array when empty using JsonDeserializer
[Android] I want to create a ViewPager that can be used for tutorials
Rails6 I want to make an array of values with a check box
[Swift5] extension that allows UIImage to be specified by a String type URL
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android