java list實現分頁


   /**
     * list分頁
     * @param list
     * @param pageNo 頁碼
     * @param pageSize 每頁多少條數據
     * @return
     */
    public static <T> List<T> splitList(List<T> list, int pageNo, int pageSize) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        int totalCount = list.size();
        pageNo = pageNo - 1;
        int fromIndex = pageNo * pageSize;
        // 分頁不能大於總數
        if(fromIndex >= totalCount) {
            return null;
        }
        int toIndex = ((pageNo + 1) * pageSize);
        if (toIndex > totalCount) {
            toIndex = totalCount;
        }
        return list.subList(fromIndex, toIndex);
    }

    /**
     * list分頁
     * @param list
     * @param page
     * @return
     */
    public static <T> List<T> splitList(List<T> list, Page page) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        int totalCount = list.size();
        int fromIndex = page.getBegin();
        // 分頁不能大於總數
        if(fromIndex >= totalCount) {
            return null;
        }
        int pageNo = (int)Math.floor((double)page.getBegin() * 1.0D / (double)page.getLength()) + 1;
        int toIndex = pageNo * page.getLength();
        if (toIndex > totalCount) {
            toIndex = totalCount;
        }
        return list.subList(fromIndex, toIndex);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM