1.可以使用一些第三方库,比如hutoo里面的CollectionUtil, 优点:不足会全部返回
int fromIndex = rows * page - rows ;
int toIndex = rows * page ;
List resultPaging = CollectionUtil.sub(list , fromIndex,toIndex);
return GyMultiResponse.ofTotal(resultPaging,list.size());
- List原生的subList方法 ,缺点:1、会出现越界问题 需要手动校验,判断list长度
int fromIndex = rows * page - rows;
int toIndex = rows * page;
if (list.size() >= toIndex){
resultPaging = list.subList(fromIndex,toIndex);
}else{
resultPaging = list.subList(fromIndex,list.size());
}
- 可以使用stream流的方式,JDK8特性,优点,不足的话会全部返回
List collect = dogList.stream().limit(10).collect(Collectors.toList());
- 用for循环截取
int counter = 0;
List resultPaging = new ArrayList<>();
for (SysExpressDto sysExpressDto : list) {
counter++;
if (counter > (rows * page - rows) && counter <= (rows * page)){
resultPaging.add(sysExpressDto);
}
}