将Map<String, List<DictResponseVO>> collect = dictTree.stream().collect(Collectors.groupingBy(DictResponseVO::getCode));的结果修改为Map<String, Map<String, List<DictResponseVO>>>
时间: 2023-07-09 12:24:16 浏览: 42
可以使用Collectors.groupingBy嵌套来完成这个转换,代码如下:
```
Map<String, Map<String, List<DictResponseVO>>> collect = dictTree.stream()
.collect(Collectors.groupingBy(DictResponseVO::getCode, Collectors.groupingBy(DictResponseVO::getType)));
```
这样就可以按照code和type两个属性进行分组,得到一个嵌套的Map。其中外层Map的key是code,value是内层Map;内层Map的key是type,value是对应的DictResponseVO列表。
相关问题
java stream流 将 List<Map<String, Object>>转换成 map的key=“code”的List集合
可以通过 Java 8 中的 Stream API 来实现将 `List<Map<String, Object>>` 转换成 `Map` 的 key 为 "code" 的 `List` 集合。
具体实现如下:
```java
List<Map<String, Object>> list = ... // 原始 List<Map> 集合
List<String> codeList = list.stream()
.map(map -> map.get("code").toString()) // 将每个 Map 中的 "code" 字段转换成 String 类型
.collect(Collectors.toList()); // 将所有 "code" 字段的值收集到 List 集合中
Map<String, List<String>> resultMap = new HashMap<>();
resultMap.put("code", codeList); // 将 List 集合作为 value,"code" 作为 key,放入 Map 中
System.out.println(resultMap); // 输出结果
```
其中,`list.stream().map(...).collect(Collectors.toList())` 用于将每个 Map 中的 "code" 字段提取出来并转换成 `List<String>` 类型。
最后,将 `List<String>` 集合作为 value,"code" 作为 key,放入 `Map` 中即可。
需要注意的是,如果原始 List<Map> 集合中存在 "code" 字段的值为 `null` 或不存在 "code" 字段,则该方法会抛出空指针异常。因此,在实际使用中需要根据实际情况进行判空处理。
代码优化 List<String> stringList = ExcelUtil.excelToShopIdList1(inputStream); if (CollectionUtils.isEmpty(stringStringMap)) { return warn("无数据"); } List<EquDeviceTslProperty> excelResult = new ArrayList<>(); stringList.forEach(ar->{ EquDeviceTslProperty equDeviceTslProperty = JSON.parseObject(ar, EquDeviceTslProperty.class); excelResult.add(equDeviceTslProperty); }); EquDeviceTslProperty deviceTslProperty = new EquDeviceTslProperty(); deviceTslProperty.setDeviceCode(excelResult.get(0).getDeviceCode()); List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(deviceTslProperty); List<EquDeviceTslProperty> saveList = new ArrayList<>(); Map<String, List<EquDeviceTslProperty>> listMap = equDeviceTslProperties.stream().collect(Collectors.groupingBy(EquDeviceTslProperty::getPropertyId)); excelResult.forEach(ar->{ List<EquDeviceTslProperty> equDeviceTslProperties1 = listMap.get(ar.getPropertyId()); if (CollectionUtils.isEmpty(equDeviceTslProperties1)) { saveList.add(ar); return; } equDeviceTslProperties1.get(0).setValueType(ar.getValueType()) .setSortsIndex(ar.getSortsIndex()) .setPropertyName(ar.getPropertyName()); equDeviceTslPropertyService.updateEquDeviceTslProperty(equDeviceTslProperties1.get(0)); }); equDeviceTslPropertyService.batchInsert(saveList);
可以考虑使用Java8的stream API来简化代码:
```
List<String> stringList = ExcelUtil.excelToShopIdList1(inputStream);
if (CollectionUtils.isEmpty(stringList)) {
return warn("无数据");
}
List<EquDeviceTslProperty> excelResult = stringList.stream()
.map(ar -> JSON.parseObject(ar, EquDeviceTslProperty.class))
.collect(Collectors.toList());
String deviceCode = excelResult.get(0).getDeviceCode();
List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(new EquDeviceTslProperty().setDeviceCode(deviceCode));
Map<String, List<EquDeviceTslProperty>> listMap = equDeviceTslProperties.stream().collect(Collectors.groupingBy(EquDeviceTslProperty::getPropertyId));
List<EquDeviceTslProperty> saveList = excelResult.stream()
.filter(ar -> CollectionUtils.isEmpty(listMap.get(ar.getPropertyId())))
.collect(Collectors.toList());
listMap.forEach((propertyId, properties) -> {
excelResult.stream()
.filter(ar -> propertyId.equals(ar.getPropertyId()))
.findFirst()
.ifPresent(ar -> {
EquDeviceTslProperty property = properties.get(0);
property.setValueType(ar.getValueType())
.setSortsIndex(ar.getSortsIndex())
.setPropertyName(ar.getPropertyName());
equDeviceTslPropertyService.updateEquDeviceTslProperty(property);
});
});
equDeviceTslPropertyService.batchInsert(saveList);
```
这样可以更清晰地表达出代码的逻辑,并且减少了一些不必要的中间变量的使用。