Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java8中检查列表是否同时为空和非空_Java_Java 8 - Fatal编程技术网

在Java8中检查列表是否同时为空和非空

在Java8中检查列表是否同时为空和非空,java,java-8,Java,Java 8,我有一个 List coins=exchange.getCoins() 我想简化下面的代码,它是在java 8以下 if (coins != null && !coins.isEmpty()) { //perform logic } 问题是,在检查之后,我有很多操作要执行,所以我想使用流方法。只需尝试使用: Stream.ofNullable(coins).filter(...).map(...).collect(..) 请尝试使用: Stream.ofNullabl

我有一个

List coins=exchange.getCoins()

我想简化下面的代码,它是在java 8以下

if (coins != null && !coins.isEmpty()) {
    //perform logic
}
问题是,在检查之后,我有很多操作要执行,所以我想使用流方法。

只需尝试使用:

Stream.ofNullable(coins).filter(...).map(...).collect(..)
请尝试使用:

Stream.ofNullable(coins).filter(...).map(...).collect(..)
可选。不可用(硬币)。如果存在(e->
e、 流()
.filter(x->x.length()>0)
.map(…)
.收集(…)
可选。不可用(硬币)。如果存在(e->
e、 流()
.filter(x->x.length()>0)
.map(…)
.收集(…)

我不明白你怎么能变得更简单。我的意思是像
coins.stream()…
我同意乔治的观点。我要补充的是,如果您是编写
getCoins
的人,那么最好确保返回一个空列表,而不是
null
,这是一种简化的方法,需要您确保
exchange.getCoins()
不返回
null
值,然后您可以在继续
coins.stream…
时忽略空检查。我不知道如何使tbh更简单。我的意思是
coins.stream()…
我同意George。我要补充的是,如果您是编写
getCoins
的人,那么最好确保返回一个空列表,而不是
null
,这是一种简化的方法,需要您确保
exchange.getCoins()
不返回
null
值,然后在继续
coins.stream…