我有一个for循环的列表,用于检查db中是否存在索引值.
如果任何值不存在,它会立即返回false.
public boolean exists(List<String> keys) {
for(String key: keys) {
boolean exists = service.existsByKey(key);
if(!exists) return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
我试图将它改为java 8 foreach,但它没有按预期工作.
keys.stream().forEach(k -> {
boolean exists = service.existsByKey(k);
if(!exists) return false;
});
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?为什么不进入if(!exists)staetment forEach()?
您的forEach方法中的return语句将被忽略.
尝试使用
boolean exists = key.stream().allMatch(k -> service.existsByKey(k));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
469 次 |
| 最近记录: |