Home > Java > javaTutorial > body text

How to Flatten Optional Values with Stream::flatMap and Java Streams?

Patricia Arquette
Release: 2024-10-23 16:35:02
Original
391 people have browsed it

How to Flatten Optional Values with Stream::flatMap and Java Streams?

Flattening Optional Values with Stream::flatMap and Java Streams

In Java 8, the Stream framework provides powerful APIs for concise code. However, certain scenarios, particularly involving Optionals, can be challenging to express succinctly.

Problem Statement

Consider a scenario where you have a list of objects and a method that resolves each object into an Optional. The goal is to map the objects to Optionals and then obtain the first non-empty value.

A straightforward approach would be to use things.stream().flatMap(this::resolve).findFirst(). However, flatMap requires returning a stream, and Optionals lack a stream() method.

Potential Solution

One solution involves using a multi-step operation:

<code class="java">things.stream()
    .map(this::resolve)
    .filter(Optional::isPresent)
    .map(Optional::get)
    .findFirst();</code>
Copy after login

While this approach works, it appears rather verbose.

Improved Solutions

Java 9 and later versions offer more elegant solutions:

Java 16:

<code class="java">Optional<Other> result =
    things.stream()
          .map(this::resolve)
          .<Other>mapMulti(Optional::ifPresent)
          .findFirst();</code>
Copy after login

Java 9:

<code class="java">Optional<Other> result =
    things.stream()
          .map(this::resolve)
          .flatMap(Optional::stream)
          .findFirst();</code>
Copy after login

Java 8 (Workaround):

Prior to Java 9, a helper function can be employed:

<code class="java">static <T> Stream<T> streamopt(Optional<T> opt) {
    return opt.isPresent() ? Stream.of(opt.get()) : Stream.empty();
}

Optional<Other> result =
    things.stream()
          .flatMap(t -> streamopt(resolve(t)))
          .findFirst();</code>
Copy after login

These enhanced solutions provide more concise and efficient ways to handle Optionals within stream operations.

The above is the detailed content of How to Flatten Optional Values with Stream::flatMap and Java Streams?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!