Java 8 collect() only isPresent() Optional values [duplicate]

我的未来我决定 提交于 2021-01-21 00:35:42

问题


Is there a more elegant way of practically achieving this in Java 8?

list.stream()
    .map(e -> myclass.returnsOptional(e))
    .filter(Optional::isPresent)
    .map(Optional::get)
    .collect(Collectors.toList());

I'm talking about filter(Optional::isPresent) followed by map(Optional::get), I want to elegantly collect in a list only Optional results which have a value.


回答1:


In your case you can use one flatMap instead of combinations of map filter and again map. To Do that it's better to define a separate function for creating a Stream: public private static Stream<Integer> createStream(String e) to not have several lines of code in lambda expression.

Please see my full Demo example:

 public class Demo{
    public static void main(String[] args) {
        List<String> list = Arrays.asList("1", "2", "Hi Stack!", "not", "5");
        List<Integer> newList = list.stream()
                .flatMap(Demo::createStream)
                .collect(Collectors.toList());
        System.out.println(newList);
    }

    public static Stream<Integer> createStream(String e) {
        Optional<Integer> opt = MyClass.returnsOptional(e);
        return opt.isPresent() ? Stream.of(opt.get()) : Stream.empty();
    }
}


class MyClass {
    public static Optional<Integer> returnsOptional(String e) {
        try {
            return Optional.of(Integer.valueOf(e));
        } catch (NumberFormatException ex) {
            return Optional.empty();
        }
    }
}

in case returnsOptional cannot be static you will need to use "arrow" expression instead of "method reference"




回答2:


Not sure if its so different but you could just filter based on your optional instead of getting the optional and filtering next. Something like this?

list.stream()
    .filter(e -> myclass.returnsOptional(e).isPresent())
    .collect(Collectors.toList());

Note: This will only work if returnsOptional returns the same object type as your original list item types.



来源:https://stackoverflow.com/questions/39920542/java-8-collect-only-ispresent-optional-values

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