使用Java流anymatch和筛选器进行空检查
我正在使用Stream.anymatch检查这四个字符串中的任何一个是空的还是空的-作为后续逻辑检查的一部分,我们可以获得空字符串列表的句柄吗使用Java流anymatch和筛选器进行空检查,java,java-8,java-stream,Java,Java 8,Java Stream,我正在使用Stream.anymatch检查这四个字符串中的任何一个是空的还是空的-作为后续逻辑检查的一部分,我们可以获得空字符串列表的句柄吗 if(Stream.of(stringA, stringB,stringC, stringD) .anyMatch(field -> field == null || field.trim().isEmpty())) 我建议您使用ApacheCommons库中的StringUtils.isBlank() 然后,可以使用临时值来存储
if(Stream.of(stringA, stringB,stringC, stringD)
.anyMatch(field -> field == null || field.trim().isEmpty()))
我建议您使用ApacheCommons库中的StringUtils.isBlank() 然后,可以使用临时值来存储具有空值的对象
List<String> blank = Stream.of(stringA, stringB,stringC, stringD)
.filter(StringUtils::isBlank)
.collect(Collectors.toList());
if (blank.size() > 0) {
// your code here
}
List blank=Stream.of(stringA、stringB、stringC、stringD)
.filter(StringUtils::isBlank)
.collect(Collectors.toList());
if(blank.size()>0){
//你的代码在这里
}
如果需要在每种情况下执行不同的代码,我建议您使用多个if(StringUtils.isBlank(stringX)){}语句。Store
stream.filter(predicate.collect(toList())
,然后检查结果是否为空。您不能存储任何匹配项中的匹配字符串。字符串列表中的“句柄”是什么意思?你想要有空值的变量名??如何存储非空字符串,从中你可以找到列表中不包含的字符串?我同意@ChotaBheem。你能确认你想知道哪些字符串不是空的吗?为什么,你要用一个空列表做什么?您是否尝试过收集器.partitioning by(field->field==null | | | field.trim().isEmpty())
?您还可以使用流API中的更多方法,如anyMatch
,noneMatch
,就像Sajna在问题中所做的那样,并将其与filter()
调用中显示的方法引用相结合