如何在返回带条件的布尔值的方法上使用流 [英] How to use stream on method that return boolean value with condition
本文介绍了如何在返回带条件的布尔值的方法上使用流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这种方法:
public boolean checkRowsFilterNameBy(String filter){
waitForElmContainsText(searchButton, "Search");
List<AuditRow> listRows = auditTable.getTable();
for(AuditRow row : listRows){
if(!row.nameStr.equals(filter)||!row.nameStr.contains(filter))
return false;
}
return true;
}
并且我希望能够使用Stream对其进行更改,我尝试了以下操作,但是我缺少了一些东西:
and I want to be able to change it using Stream , I've tried the following, but I am missing something:
listRows.stream().forEach(auditRow -> {
if(auditRow.actionStr.equals(filter))return true;} else return false;);
但是我遇到了错误.
推荐答案
您可以这样做,
listRows.stream().allMatch(row -> row.nameStr.equals(filter) && row.nameStr.contains(filter));
更新
按照Holgers的建议,这可以进一步简化.
As per Holgers suggestion, this can be further simplified as this.
listRows.stream().allMatch(row -> row.nameStr.contains(filter));
equals
或contains
的使用可能会因您的上下文而异.
The use of equals
or contains
may vary depending on your context.
这篇关于如何在返回带条件的布尔值的方法上使用流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文