嵌套地图上的流

我有以下用例。我有一个具有以下结构的嵌套地图:


Map<String, Map<WorkType, List<CostLineItem>>>

我必须遍历地图并获取 CLObject 的列表。如果列表中的单个条目的标识符为 null。我必须为每个 EnumType 生成唯一标识符。我不确定如何使用流来做到这一点?遵循迭代逻辑将明确我想要完成的任务


for(Map.Entry<String, Map<WorkType, List<CostLineItem>>> cliByWorkTypeIterator: clisByWorkType.entrySet()) {

       Map<WorkType, List<CostLineItem>> entryValue = cliByWorkTypeIterator.getValue();

       for(Map.Entry<WorkType, List<CostLineItem>>cliListIterator : entryValue.entrySet()) {

           List<CostLineItem> clis = cliListIterator.getValue();

           //if any CLI settlementNumber is zero this means we are in standard upload

           //TODO: Should we use documentType here? Revisit this check while doing dispute file upload

           if(clis.get(0).getSettlementNumber() == null) {

               clis.forEach(f -> f.toBuilder().settlementNumber(UUID.randomUUID().toString()).build());

           }

       }

   } 

嵌套循环使代码位样板和脏。有人可以帮我处理这里的流吗?


陪伴而非守候
浏览 121回答 3
3回答

交互式爱情

您可以使用flatMap迭代List<CostLineItem>所有内部Maps的所有值。clisByWorkType.values() // returns Collection<Map<WorkType, List<CostLineItem>>>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream() // returns Stream<Map<WorkType, List<CostLineItem>>>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(v->v.values().stream()) // returns Stream<List<CostLineItem>>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(clis -> clis.get(0).getSettlementNumber() == null) // filters that Stream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(clis -> {do whatever logic you need to perform on the List<CostLineItem>});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java