Map<Integer, List<ProductListOut>> typeMap = pageList.getRecords().stream().collect(Collectors.groupingBy(ProductListOut::getTypeId,
Collectors.collectingAndThen(Collectors.toList(),productListOuts -> {
Integer toIndex = infoVo.getPageSize();
if(productListOuts.size() < toIndex){
toIndex = productListOuts.size();
}
return productListOuts.subList(0,toIndex);
})));
使用方法 先使用 Collectors.groupingBy()进行分组 ,分组的同时调用
Collectors.collectingAndThen(Collectors.toList(),productListOuts -> {
Integer toIndex = infoVo.getPageSize();
//判断要截取值是否大于当前集合数 如果大于则使用当前集合长度
if(productListOuts.size() < toIndex){
toIndex = productListOuts.size();
}
//list集合截取
return productListOuts.subList(0,toIndex);
});
实现集合截取固定长度 返回值 。 完毕
这段代码展示了如何使用Java 8的Stream API对Map<Integer, List<ProductListOut>>进行分组,并根据指定的页面大小截取每个分组内列表的元素。通过对ProductListOut按类型ID分组,然后收集并调整每个列表的大小,确保每个子列表不超过给定的页面大小限制。
4309

被折叠的 条评论
为什么被折叠?



