将List<BomInventory> topTenAddList转化成List<BomDashboardVo> list1并且按照存货分类字段进行统计个数
时间: 2024-03-05 07:55:12 浏览: 73
好的,根据您的要求,可以使用Java Stream API来进行转化和统计操作。具体代码如下:
```
List<BomInventory> topTenAddList = ... // 假设已经有了 topTenAddList
List<BomDashboardVo> list1 = topTenAddList.stream()
.collect(Collectors.groupingBy(BomInventory::getInventoryCategory)) // 根据存货分类字段进行分组
.entrySet().stream()
.map(entry -> {
BomDashboardVo vo = new BomDashboardVo();
vo.setInventoryCategory(entry.getKey()); // 设置存货分类字段
vo.setCount(entry.getValue().size()); // 设置统计个数
return vo;
})
.collect(Collectors.toList()); // 转化为 List<BomDashboardVo>
```
上述代码中,我们首先使用 `groupingBy` 方法将 `topTenAddList` 按照 `BomInventory` 对象的 `inventoryCategory` 字段进行分组,得到一个 `Map<String, List<BomInventory>>` 类型的结果。然后,我们使用 `entrySet` 方法将其转化为 `Set<Map.Entry<String, List<BomInventory>>>>` 类型的集合,以便进行下一步的操作。
接下来,我们使用 `map` 方法将每个 `Map.Entry<String, List<BomInventory>>` 对象转化为一个 `BomDashboardVo` 对象,其中 `inventoryCategory` 字段设置为该条目的键(即存货分类字段),`count` 字段设置为该条目的值(即 `List<BomInventory>` 的大小,即统计个数)。最后,我们使用 `toList` 方法将所有 `BomDashboardVo` 对象转化为一个 `List<BomDashboardVo>` 类型的结果。
阅读全文
相关推荐

















