可以使用stream流的collect方法结合Collectors.groupingBy来实现根据某个字段将List externalInfos收集成Map<Long,List>。假设需要根据字段'field'进行分组,可以按照以下方式实现:

Map<Long, List<Long>> resultMap = externalInfos.stream()
        .collect(Collectors.groupingBy(BillExternalInfo::getField,
                Collectors.mapping(BillExternalInfo::getId, Collectors.toList())));

上述代码中,使用Collectors.groupingBy方法将List按照字段'field'进行分组,并使用Collectors.mapping方法将每个BillExternalInfo对象的'id'字段提取出来,最终收集成List。最终得到的结果是一个Map<Long, List>,其中键是字段'field'的值,值是对应字段值的所有BillExternalInfo对象的'id'字段组成的列表。

使用Stream流将List<BillExternalInfo> 按字段分组为Map<Long, List<Long>>

原文地址: https://www.cveoy.top/t/topic/qjux 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录