[英]Check if a List is not empty and not null simultaneously in java 8
I have a我有一个
List<Object> coins = exchange.getCoins();
I want to simplify the below code which is in java below 8.我想简化下面的代码,它在 java 中低于 8。
if (coins != null && !coins.isEmpty()) {
//perform logic
}
The thing is that after the check I have lot of operation to perform, so I want to have a stream approach.问题是检查后我有很多操作要执行,所以我想要一个 stream 方法。
Just try use:只需尝试使用:
Stream.ofNullable(coins).filter(...).map(...).collect(..)
Optional.ofNullable(coins).ifPresent(e-> e.stream() .filter(x-> x.length()>0) .map(...) .collect(...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.