在Java8流中,我可以使用该mapToInt方法创建一个IntStream,它将OptionalInt为某些动作返回s(如findFirst).为什么没有类似的东西Optional?
int i = Stream
.of("1") // just as an example
.mapToInt(Integer::parseInt) // mapToInt exists for streams
.findFirst() // this even returns an OptionalInt!
.getAsInt(); // quite handy
int j = Optional
.of("1") // same example
.map(Integer::parseInt) // no mapToInt available
.get().intValue(); // not as handy as for streams
Run Code Online (Sandbox Code Playgroud) 所以我现在已经使用Guava的Optional一段时间了,我转向Java 8,所以我想使用它的Optional类,但它没有我最喜欢的方法来自Guava,asSet().有没有办法用Java 8 Optional执行此操作,我没有看到.我喜欢将可选项视为一个集合,所以我可以这样做:
for( final User u : getUserOptional().asSet() ) {
return u.isPermitted(getPermissionRequired());
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,避免需要额外的变量.
IE
Optional<User> optUser = getUserOptional();
if ( optUser.isPresent() ) {
return optUser.get().isPermitted(getPermissionRequired());
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来复制Java 8的可选中的番石榴风格?
谢谢