Java 8 collect() only isPresent() Optional values [duplicate]
问题 This question already has answers here : Using Java 8's Optional with Stream::flatMap (12 answers) Closed 4 years ago . 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