Skip to content

Stream操作

List<?> 转 Map<String, List<?>>

集合中,根据某个字段分组,泛型一样:List<T> 转 Map<String, List<T>>

java
List<XxxxxDTO> resultList = new ArrayList<>();
Map<String, List<XxxxxDTO>> = riskLiabilityCoreReleDTOS.stream().collect(
    Collectors.groupingBy(
        XxxxxDTO::getCode,
        Collectors.mapping(
          Function.identity(),
          Collectors.toList()
        )
    )
);

List<A> 转 Map<String, List<B>>

集合中,根据某个字段分组,泛型不一样:List<A> 转 Map<String, List<B>>

java
List<XxxxxDTO> resultList = new ArrayList<>();
Map<String, List<SysDictData>> = resultList.stream().collect(
  Collectors.groupingBy(
    XxxxxDTO::getCode,
      Collectors.mapping(
          v -> {
              SysDictData sysDictData = new SysDictData();
              sysDictData.setDictLabel(v.getGetxxxxxname());
              sysDictData.setDictValue(v.getGetxxxxxcode());
              sysDictData.setListClass("");
              return sysDictData;
          },
          Collectors.toList()
      )
    )
);