Java實現(xiàn)List集合手動分頁的方法
為方便測試,可以直接在 controller 內(nèi)添加一個方法,或者直接通過 main 方法測試
List 手動分頁
@GetMapping("/getUserInfo")
public Map<String,Object> getUserInfo(@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){
if(null == pageNo){
pageNo = 1;
}
if(null == pageSize){
pageSize = 10;
}
List<User> userList = new ArrayList<>();
userList.add(new User(1,"奧迪1",23,"123@qq.com","地址1"));
userList.add(new User(2,"奧迪2",23,"124@qq.com","地址2"));
userList.add(new User(3,"奧迪3",23,"124@qq.com","地址3"));
userList.add(new User(4,"奧迪4",23,"125@qq.com","地址4"));
userList.add(new User(5,"奧迪4",23,"126@qq.com","地址5"));
userList.add(new User(6,"奧迪5",23,"127@qq.com","地址6"));
userList.add(new User(7,"奧迪6",23,"128@qq.com","地址7"));
userList.add(new User(8,"奧迪7",23,"129@qq.com","地址8"));
userList.add(new User(9,"奧迪7",23,"1219@qq.com","地址9"));
userList.add(new User(10,"奧迪10",23,"1231@qq.com","地址10"));
userList.add(new User(11,"奧迪11",23,"1232@qq.com","地址11"));
Integer limit = (pageNo-1)*pageSize;
Integer totalSize = userList.size();
// 方法一
Integer totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
// 方法二(推薦)其中 pageSize-1 就是 totalSize/pageSize 的最大的余數(shù)
// Integer totalPage = (totalSize + pageSize -1) / pageSize;
// 方法三
// Integer totalPage = (int) Math.ceil(totalSize / pageSize);
if(pageNo > totalPage){
throw new RuntimeException("頁數(shù)超出了");
}
List<User> subList = null;
if(pageNo == totalPage){
// 最后一頁
subList = userList.subList(limit, totalSize);
}else{
Integer end= limit + pageSize;
// 截取的最后的下標
subList = userList.subList(limit, end);
// 出現(xiàn)空值處理
// subList = CollectionUtils.isEmpty(userList) ? new ArrayList<>() : userList.subList(limit, Math.min(userList.size(), end));
}
Map<String,Object> map = new HashMap<>();
map.put("data",subList);
map.put("total",userList.size());
subList.forEach(System.out::println);
return map;
}
static class User{
private Integer id;
private String name;
private Integer age;
private String email;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User(Integer id, String name, Integer age, String email, String address) {
this.id = id;
this.name = name;
this.age = age;
this.email = email;
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
", address='" + address + '\'' +
'}';
}
}
計算總頁數(shù)方法
// totalPage 總頁數(shù),pageSize 每頁顯示多少條,totalSize 總條數(shù) // 方法一 Integer totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1; // 方法二(推薦)其中 pageSize-1 就是 totalSize/pageSize 的最大的余數(shù) // Integer totalPage = (totalSize + pageSize -1) / pageSize; // 方法三 // Integer totalPage = (int) Math.ceil(totalSize / pageSize);
補:
利用 Lists.partition() 方法進行數(shù)據(jù)將數(shù)據(jù)切割分頁
List<UserEntity> resultList = new ArrayList<>(); // 將數(shù)據(jù)按照傳過來頁長進行切割
List<List<UserEntity>> partition = Lists.partition(userList,pageSize);
for (int i = 0; i < partition.size(); i++) {
if (i == pageIndex){
resultList = partition.get(pageIndex);
}
}利用封裝的分頁工具進行分頁
(1)分頁工具
/**
* @project
* @Description 對List集合進行份分頁
* @Author songwp
* @Date 2023/5/12 13:55
**/
public class ListPagingUtil {
private Integer currentPage;//當前頁
private Integer pageSize;//每頁顯示記錄條數(shù)
private Integer totalPage;//總頁數(shù)
private Integer star;//開始數(shù)據(jù)
private Integer total;//總條數(shù)
private List<?> dataList;//每頁顯示的數(shù)據(jù)
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<?> getDataList() {
return dataList;
}
public void setDataList(List<?> dataList) {
this.dataList = dataList;
}
public Integer getStar() {
return star;
}
public void setStar(Integer star) {
this.star = star;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
@Override
public String toString() {
return "ListPagingUtil{" +
"currentPage=" + currentPage +
", pageSize=" + pageSize +
", totalPage=" + totalPage +
", dataList=" + dataList +
", star=" + star +
", total=" + total +
'}';
}
public void pageStartInfo(Integer currentPage, Integer pageSize){
//如果傳入的pageNumber為null給pageNumber賦為1
currentPage = currentPage == null ? 1 : currentPage;
//如果傳入的pageSize為null給pageSize賦為10
pageSize = pageSize == null ? 10 : pageSize;
this.setCurrentPage(currentPage);
this.setPageSize(pageSize);
}
public static ListPagingUtil paging(Integer currentPage, Integer pageSize, List<?> list) {
ListPagingUtil pagingUtil = new ListPagingUtil();
//初始化
pagingUtil.pageStartInfo(currentPage, pageSize);
//設(shè)置起始數(shù)據(jù)
pagingUtil.setStar((pagingUtil.getCurrentPage()-1)*pagingUtil.getPageSize());
//設(shè)置總數(shù)
pagingUtil.setTotal(list.size());
//設(shè)置總頁數(shù)
pagingUtil.setTotalPage(pagingUtil.getTotal() % pagingUtil.getPageSize() == 0 ? pagingUtil.getTotal()/pagingUtil.getPageSize() :pagingUtil.getTotal()/pagingUtil.getPageSize()+1);
//截取list
pagingUtil.setDataList(list.subList(pagingUtil.getStar(), pagingUtil.getTotal()-pagingUtil.getStar()>pagingUtil.getPageSize()?pagingUtil.getStar()+pagingUtil.getPageSize():pagingUtil.getTotal()));
return pagingUtil;
}
}(2)方法調(diào)用
ListPagingUtil.paging(pageIndex,pageSize, userList)
利用 stream進行分頁 處理
方法一:
List<List<CompanyStaffEquipStatisticVO>> partition = ListUtils.partition(result, param.getPageSize());
List<CompanyStaffEquipStatisticVO> companyStaffEquipStatisticVOS = param.getPageIndex() > partition.size() ? new ArrayList<>() : partition.get(param.getPageIndex() - 1);
SafePageParam safePageParam = new SafePageParam();
safePageParam.setPage(true);
safePageParam.setPageSize(param.getPageSize());
PageVO<CompanyStaffEquipStatisticVO> voResult = new PageVO<>(safePageParam, companyStaffEquipStatisticVOS, result.size());
if (param.getPageIndex()*param.getPageSize()<result.size()) {
voResult.setHasNext(true);
}
return voResult;方法二:
List<VendorExcelVO> resultVO = getVendorProjectListInfo(param);
PageVO<VendorExcelVO> voPageVO = new PageVO<>();
List<VendorExcelVO> collectList = resultVO.stream().skip((param.getPageIndex() - 1) * param.getPageSize()).limit(param.getPageSize()).collect(Collectors.toList());
voPageVO.setPage(true);
voPageVO.setPageSize(param.getPageSize());
voPageVO.setCount(resultVO.size());
voPageVO.setList(collectList);
if (param.getPageIndex() * param.getPageSize() < resultVO.size()){
voPageVO.setHasNext(true);
}
return voPageVO;到此這篇關(guān)于Java實現(xiàn)List集合手動分頁的方法的文章就介紹到這了,更多相關(guān)Java List集合手動分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用編程方式配置DataSource的方法
這篇文章主要介紹了SpringBoot使用編程方式配置DataSource的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
詳解Java弱引用(WeakReference)的理解與使用
這篇文章主要介紹了Java弱引用(WeakReference)的理解與使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
java 使用Scanner類接收從控制臺輸入的數(shù)據(jù)方式
這篇文章主要介紹了java 使用Scanner類接收從控制臺輸入的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
IDEA 2020.3.X 創(chuàng)建scala環(huán)境的詳細教程
這篇文章主要介紹了IDEA 2020.3.X 創(chuàng)建scala環(huán)境的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

