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);
});
实现集合截取固定长度 返回值 。 完毕