使用Set <map <string,object =“”>&gt;转换子句to to each forEach

时间:2016-09-28 15:23:48

标签: for-loop foreach java-8

我有以下方法:

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子句?有可能吗?

1 个答案:

答案 0 :(得分:0)

不赞成更新流中的共享可变变量。这应该做到。

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"));
      });
});