//使用@Nullable注解表示key和value可能为null HashMap<@Nullable String, @Nullable List> map = list.stream() //过滤掉personTypeId为空的元素 .filter(patrolGroupPersonDTO -> !CollectionUtils.isEmpty(patrolGroupPersonDTO.getPersonTypeId())) //将每个元素的personTypeId拆分成多个元素,并和原元素一起封装到一个SimpleEntry对象中 .flatMap(patrolGroupPersonDTO -> patrolGroupPersonDTO.getPersonTypeId().stream() .map(type -> new AbstractMap.SimpleEntry<>(type, patrolGroupPersonDTO))) //按照key分组,将同一key对应的value放到一个List中,最终生成一个HashMap .collect(Collectors.groupingBy(AbstractMap.SimpleEntry::getKey, HashMap::new, Collectors.mapping(AbstractMap.SimpleEntry::getValue, Collectors.toList())))

HashMapNullable String Nullable ListPatrolGroupPersonDTO map = liststream filterpatrolGroupPersonDTO - !CollectionUtilsisEmptypatrolGroupPersonDTOgetPersonTypeId flatMappatrolGroupPerson

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

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