Flattening a list of elements in Java 8 Optional pipeline

做~自己de王妃 提交于 2020-12-29 13:19:54

问题


I have a id value which can be null. Then I need to call some service with this id to get a list of trades and fetch the first not null trade from the list.

Currently I have this working code

Optional.ofNullable(id)
    .map(id -> service.findTrades(id))
    .flatMap(t -> t.stream().filter(Objects::nonNull).findFirst())
    .orElse(... default value...); 

Is it possible to implement a line with a flatMap call more elegantly? I don't want to put much logic in one pipeline step.

Initially I expected to implement the logic this way

Optional.ofNullable(id)
    .flatMap(id -> service.findTrades(id))
    .filter(Objects::nonNull)
    .findFirst()
    .orElse(... default value...); 

But Optional.flatMap doesn't allow to flatten a list into a set of it's elements.


回答1:


I don't know if this is elegant or not, but here's a way to transform the optional in a stream before initiating the stream pipeline:

Trade trade = Optional.ofNullable(id)
    .map(service::findTrades)
    .map(Collection::stream)
    .orElse(Stream.empty()) // or orElseGet(Stream::empty)
    .filter(Objects::nonNull)
    .findFirst()
    .orElse(... default value...); 

In Java 9, Optional will have a .stream() method, so you will be able to directly convert the optional into a stream:

Trade trade = Optional.ofNullable(id)
    .stream() // <-- Stream either empty or with the id
    .map(service::findTrades) // <-- Now we are at the stream pipeline
    .flatMap(Collection::stream) // We need to flatmap, so that we
    .filter(Objects::nonNull)    // stream the elements of the collection
    .findFirst()
    .orElse(... default value...); 



回答2:


There is a better way to do it by StreamEx

StreamEx.ofNullable(id)
    .flatMap(id -> service.findTrades(id))
    .filter(Objects::nonNull)
    .findFirst()
    .orElse(... default value...); 

I just saw: "As Stuart Marks says it, Rule #4: It's generally a bad idea to create an Optional for the specific purpose of chaining methods from it to get a value.." in the comments under another question:



来源:https://stackoverflow.com/questions/44578449/flattening-a-list-of-elements-in-java-8-optional-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!