Java Optional.map() error: "Cannot infer type argument(s) for <U> map(Function<? super T,? extends U>)"
Java Optional.map() error: "Cannot infer type argument(s) for <U> map(Function<? super T,? extends U>)"
我最近做了更多的 Scala,现在我锻炼了一点 Java 8.
这是代码:
Optional<String> bikeOpt2 = Optional.ofNullable("Pegas");
out.println(bikeOpt2);
bikeOpt2.map(out::println); // at this point Eclipse underlines in red and says "Cannot infer type argument(s) for <U> map(Function<? super T,? extends U>)"
应该怎么写才能编译?
map
应仅在有 Optional
结果时使用。
你要找的是
bikeOpt2.ifPresent(out::println);
我最近做了更多的 Scala,现在我锻炼了一点 Java 8. 这是代码:
Optional<String> bikeOpt2 = Optional.ofNullable("Pegas");
out.println(bikeOpt2);
bikeOpt2.map(out::println); // at this point Eclipse underlines in red and says "Cannot infer type argument(s) for <U> map(Function<? super T,? extends U>)"
应该怎么写才能编译?
map
应仅在有 Optional
结果时使用。
你要找的是
bikeOpt2.ifPresent(out::println);