Get a non-empty collection from an Optional stream in java

Trying to get a non-empty stream from a Optional stream in java was pretty tedious. I had no choice but to do my best with `filter ()` or `flatMap ()` as in https://www.baeldung.com/java-filter-stream-of-optional. However, it became much easier with the addition of Optional :: stream from java 9.

Now, suppose that there is a function that returns ```Optional.empty () `` `if it is an empty string, as shown below. Suppose you want to apply this to each element of a string list to get a list of non-empty characters.

	static Optional<String> map(String s) {
		if (s.length() == 0) {
			return Optional.empty();
		}
		return Optional.of(s);
	}

It's like this to work hard with filter </ code>.

		List.of("1", "2", ,"", "3").stream()
			.map(Hoge::map)
			.filter(Optional::isPresent)
			.map(Optional::get)
		    .collect(Collectors.toList());

Using the java 9 Optional :: stream, this is the case.

		List.of("1", "2", "", "3").stream()
			.map(Hoge::map)
			.flatMap(Optional::stream)
			.collect(Collectors.toList());

Recommended Posts

Get a non-empty collection from an Optional stream in java
Easily get an integer from a system property in Java
[Java] Get a random value from an array
Get attributes and values from an XML file in Java
Get stuck in a Java primer
Get history from Zabbix server in Java
Java 9 Optional :: stream
GetInstance () from a @Singleton class in Groovy from Java
How to get Class from Element in Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Call a program written in Swift from Processing (Java)
[Android development] Get an image from the server in Java and set it in ImageView! !!
Get weather forecasts from Watson Weather Company Data in simple Java
Java Stream API in 5 minutes
Find a subset in Java
How to store a string from ArrayList to String in Java (Personal)
[Java] Get KClass in Java [Kotlin]
What is a Java collection?
Use Redis Stream in Java
Take a look at Kotlin from an Effective Java perspective
Deep copy collection in Java
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
[Note] What I learned in half a year from inexperienced (Java) (1)
Cast an array of Strings to a List of Integers in Java
[Note] What I learned in half a year from inexperienced (Java) (3)
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
Effective Java Item 25 Select a list from an array First half
I sent an email in Java
3 Implement a simple interpreter in Java
I created a PDF in Java.
Get country from IP address (Java)
Get Null-safe Map values in Java
Run a batch file from Java
Access Teradata from a Java application
Try an If expression in Java
A simple sample callback in Java
I made an annotation in Java.
Do you use Stream in Java?
Run an external process in Java
I want to ForEach an array with a Lambda expression in Java
[Java] Generate a narrowed list from multiple lists using the Stream API
When calling println etc. from an external Java class file in Processing
Study Deep Learning from scratch in Java.
When seeking multiple in a Java array
Get the result of POST in Java
Significance of interface learned from Java Collection
Data processing using stream API from Java 8
Call Java method from JavaScript executed in Java
Try using the Stream API in Java
Get caller information from stack trace (java)
Map without using an array in java
Reverse Key from Value in Java Map
[Creating] A memorandum about coding in Java
Using JavaScript from Java in Rhino 2021 version
Java creates a table in a Word document
Java creates a pie chart in Excel
What is a class in Java language (2 /?)
Change List <Optional <T >> to Optional <List <T >> in Java
Try running a Kubernetes Job from Java