如何将此源代码转换为 lambda?

它由列表对象中的地图组成。我尝试通过循环语句比较它们来匹配具有相同 ID 的列表。如何转换为 lambda?


List<Map<String, String>> combineList = new ArrayList<>(); // Temp List

for(Map titleMap : titleList) { // Name List

   for(Map codeMap : codeList) { // Age List

      if(titleMap.get("ID").equals(codeMap.get("ID"))) { // compare Id

         Map<String,String> tempMap = new HashMap<>();

          tempMap.put("ID", titleMap.get("ID"));

          tempMap.put("NAME", titleMap.get("NAME"));

          tempMap.put("AGE", codeMap.get("AGE"));

          combineList.add(tempMap);

      }

   }

}


HUH函数
浏览 82回答 1
1回答

小怪兽爱吃肉

您已经在以有效的方式进行操作。因此,如果您愿意,可以将相同的代码更改为仅使用stream().forEach,或者如果想更多地使用流,请执行以下操作:titleList.stream()&nbsp; &nbsp; &nbsp; &nbsp; .forEach(titleMap ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; combineList.addAll(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codeList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(codeMap -> titleMap.get("ID").equals(codeMap.get("ID")))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(codeMap -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> tempMap = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMap.put("ID", titleMap.get("ID"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMap.put("NAME", titleMap.get("NAME"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMap.put("ID", codeMap.get("ID"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMap.put("AGE", codeMap.get("AGE"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return tempMap;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );请注意,您每次都必须从 codeList 中进行过滤,因为您的条件就是这样。尝试使用类来代替 Map 更高效、更清洁和更有效。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java