处理list中ID相同的对象合并,并将某一个值相加问题
List<SuppliesWarehousingInventory> resList = new ArrayList<>(suppliesOutboundInventoryList.stream()
.collect(Collectors.toMap(SuppliesWarehousingInventory::getId,
a -> a,
(o1, o2) -> { o1.setWarehousingNumber(o1.getWarehousingNumber().add(o2.getWarehousingNumber()));
return o1;
})).values());
处理list 中重复的数据根据id
List<String> ids = list.stream().map(SuppliesMaterialStock::getId).distinct().
collect(Collectors.toList());
获取list的id合集
List<String> userIds = userList.stream().map(SysUser::getId).collect(Collectors.toList());
返回list 中包含当前id 的数据
List<String> selectionList = Arrays.asList(selections.split(","));
queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
一个列表里筛选比较重的苹果
顺序处理:
List<Apple> heavyApples =
inventory.stream().filter((Apple a) -> a.getWeight() > 150)
.collect(Collectors.toList());
并行处理:
List<Apple> heavyApples =
inventory.parallelStream().filter((Apple a) -> a.getWeight() > 150)
.collect(Collectors.toList());
多属性去重
List<PropertyHouseExcelExport> projectList = list.stream().collect(Collectors.collectingAndThen
(Collectors.toCollection(() -> new TreeSet<PropertyHouseExcelExport>(
Comparator.comparing(p -> p.getProjectCode() + ";" + p.getProjectName()))), ArrayList::new));
List 转换为 Map
Map<String, String> projectMap = projectList.stream()
.collect(Collectors.toMap(PropertyCommunityProject::getName, PropertyCommunityProject::getId));