zoukankan      html  css  js  c++  java
  • 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);
        }
    随笔看心情
  • 相关阅读:
    iBatis系列一
    iBatis入手案例
    需求分析7字诀
    自我意识为王
    物联网关键技术之一
    物联网之二:传感器无线网络
    物联网之二:传感器无线网络
    物联网时代之一
    项目经理的心法
    项目管理规划
  • 原文地址:https://www.cnblogs.com/stromgao/p/14246729.html
Copyright © 2011-2022 走看看