Skip to content

Instantly share code, notes, and snippets.

@guozi
Created December 8, 2021 07:42
Show Gist options
  • Save guozi/6cefae5eb9d83c72103ccfffbed9d9ca to your computer and use it in GitHub Desktop.
Save guozi/6cefae5eb9d83c72103ccfffbed9d9ca to your computer and use it in GitHub Desktop.
使用Pageable将查询结果list转page分页
class ListPageUtils {
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
for (int i=0;i<20;i++){
strings.add("第"+i+"数据");
}
Pageable pageRequest = PageRequest.of(1, 10);
Page<String> strings1 = listConvertToPage(strings, pageRequest);
System.out.println(strings1);
}
private static <T> Page<T> listConvertToPage(List<T> list, Pageable pageable) {
final int start = (int) pageable.getOffset();
final int end = Math.min((start + pageable.getPageSize()), list.size());
return new PageImpl<T>(list.subList(start, end), pageable, list.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment