问题
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