我有以下用例。我有一个具有以下结构的嵌套地图: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()); } } } 嵌套循环使代码位样板和脏。有人可以帮我处理这里的流吗?
3 回答
![?](http://img1.sycdn.imooc.com/545862e700016daa02200220-100-100.jpg)
交互式爱情
TA贡献1712条经验 获得超3个赞
您可以使用flatMap迭代List<CostLineItem>所有内部Maps的所有值。
clisByWorkType.values() // returns Collection<Map<WorkType, List<CostLineItem>>>
.stream() // returns Stream<Map<WorkType, List<CostLineItem>>>
.flatMap(v->v.values().stream()) // returns Stream<List<CostLineItem>>
.filter(clis -> clis.get(0).getSettlementNumber() == null) // filters that Stream
.forEach(clis -> {do whatever logic you need to perform on the List<CostLineItem>});
添加回答
举报
0/150
提交
取消