将 Set<Map<String、Object>> 的子句转换为 for Each 的子句



我有以下方法:

protected Set<Map<String, Object>> joinQueryResults(Set<Map<String, Object>> resultEmpty, Set<Map<String, Object>> resultWithValues){
    for(Map<String, Object> mapEmpty: resultEmpty){         
        for(Map<String, Object> mapValue: resultWithValues){
            if (mapValue.get("name").equals(mapEmpty.get("name"))){
                mapEmpty.replace("totfailed", mapValue.get("totfailed"));
                mapEmpty.replace("totsuccess", mapValue.get("totsuccess"));
                break;
            }       
        }   
    }       
    return resultEmpty; 
}

如何将其转换为forEach子句?这可能吗?

不允许在流中更新共享可变变量。这个应该可以了。

resultEmpty.stream().forEach(empty -> {
  resultWithValues.stream()
      .filter(
          withValues -> withValues.get("name").equals(empty.get("name")))
      .findFirst().ifPresent(withValues -> {
        empty.replace("totsuccess", withValues.get("totsuccess"));
        empty.replace("totfailed", withValues.get("totfailed"));
      });
});

最新更新