java - 使用两个 for 循环简化计数(使用流)
问题描述
我有这段代码,我想使用流来编写它。我需要检查 hList 是否包含所有 dFoods 元素。
int count = 0;
for(int i = 0; i< dFoods.size(); i++){
for(int j = 0; j< hList.size(); j++){
if(hList.get(j).title.equals(dFoods.get(i).name) && hList.get(j).time.equals(dFoods.get(i).timestamp)){
count ++;
}
}
}
if(count != dFoods.Elements.size()){
System.out.println("Not all dFoods elements are in a hList");
}
我试过了
dFoods.forEach(df -> {
hList.stream().filter(hl -> df.Name.equals(hl.title) && df.Timestamp.equals(hl.time)).forEach(hl -> {
System.out.println(df.Name + " " + df.Timestamp);
});
});
它写得正确,但我需要数数,但不能这样做。
解决方案
dFoods.stream()
.allMatch(df ->
hList.stream
.anyMatch(hl -> df.Name.equals(hl.title) && df.Timestamp.equals(hl.time)))
推荐阅读
- json - URL 参数使用 OR 过滤 JSON
- r - 有没有办法在 R 中对 ChainLadder 损失三角形的列进行切片?
- python - sklearn kmeans忽略集群的数量
- firebase - Firestore 子集合 ID
- c++ - Does anybody know how to link against `ld` on `WSL`?
- javascript - 科尔多瓦如何告诉浏览器文件传输 CORS 许可
- excel - 变量在 VBA 中保持为空 Do while 迭代
- python - Keras 模型中的自定义交叉熵度量
- python - 使用 xpath 从类中提取数字
- java - Android:尽管授予读写权限,但无法打开文件(权限被拒绝)