我希望使用Stream和Lambda来构建地图。
我尝试了很多方法,但卡住了。这里是使用Stream/Lambda和经典循环两种方式的Java代码。
Map<Entity, List<Funder>> initMap = new HashMap<>();
List<Entity> entities = pprsToBeApproved.stream()
.map(fr -> fr.getBuyerIdentification().getBuyer().getEntity())
.distinct()
.collect(Collectors.toList());
for(Entity entity : entities) {
List<Funder> funders = pprsToBeApproved.stream()
.filter(fr -> fr.getBuyerIdentification().getBuyer().getEntity().equals(entity))
.map(fr -> fr.getDocuments().get(0).getFunder())
.distinct()
.collect(Collectors.toList());
initMap.put(entity, funders);
}
正如你所看到的,我只知道如何在列表中收集信息,但是我无法使用地图实现相同效果。因此,我必须再次流式传输我的列表以建立第二个列表,最终将所有内容放入一个映射中。 我也尝试了"collect.groupingBy"语句,因为它也应该生成一个映射,但是我失败了。
entities.forEach(entity-> ..... );
。 - GOXR3PLUS