Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 683
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Oct 07 '16 15:10

ᴘᴀɴᴀʏɪᴏᴛɪs


1 Answers

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"

like image 111
eGoLai Avatar answered Sep 27 '22 03:09

eGoLai