Java分页需求

    近期在实现项目接口时,经常需要分页功能。有时返回“String”,有时是“Object”格式的分页List。针对这种情况,本人用java实现对List分页。

 

第一版

package org.sun.java;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Administrator
 * @date 2016年7月16日
 */
public class SubListOperation {
    final static int COUNT = 18;

    public List<String> init(int size) {
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < size; i++) {
            lists.add("t" + (i + 1));
        }
        return lists;
    }
    
    /**
     * 
     * @param pageNum
     * @param pageSize
     * @return  简单分页功能
     */
    public List<String> subList(int pageNum, int pageSize) {
        List<String> lists = init(COUNT);
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        return lists.subList(fromIndex, toIndex);
    }

    public static void main(String[] args) {
        System.out.println("-------- start --------");
        new SubListOperation().output();
        System.out.println("-------- finish --------");
    }
    
    public void output(){
        int pageNum=2;
        int pageSize=5;
        for(String s:subList(pageNum,pageSize)){
            System.out.println(s);
        }
    }

}

-------- start --------
t6
t7
t8
t9
t10
-------- finish --------

考虑到超出元素个数时异常的情况,做多一步判断。

第二版

    
    /**
     * 
     * @param pageNum
     * @param pageSize
     * @return  简单分页功能
     */
    public List<String> subList(int pageNum, int pageSize) {
        List<String> lists = init(COUNT);
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        int size = lists.size();
        if(fromIndex>=size){
            return new ArrayList<String>();
        }
        return lists.subList(fromIndex, Math.min(size,toIndex));
    }

第三版
package org.sun.java;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Administrator
 * @date 2016年7月16日
 */
public class SubListOperation {
    final static int COUNT = 18;

    public List<String> init(int size) {
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < size; i++) {
            lists.add("t" + (i + 1));
        }
        return lists;
    }

    /**
     * 
     * @param size
     * @return 初始化用户列表
     */
    public List<User> initUser(int size) {
        List<User> lists = new ArrayList<User>();
        for (int i = 0; i < size; i++) {
            lists.add(new User((i + 1), "user" + (i + 1)));
        }
        return lists;
    }

    
    
    /**
     * 
     * @param pageNum
     * @param pageSize
     * @return 简单分页功能
     */
    public List<User> userSubList(int pageNum, int pageSize) {
        List<User> lists = initUser(COUNT);
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        int size = lists.size();
        if (fromIndex >= size) {
            return new ArrayList<User>();
        }
        return lists.subList(fromIndex, Math.min(size, toIndex));
    }

    
    
    public List<String> subList(int pageNum, int pageSize) {
        List<String> lists = init(COUNT);
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        int size = lists.size();
        if (fromIndex >= size) {
            return new ArrayList<String>();
        }
        return lists.subList(fromIndex, Math.min(size, toIndex));
    }
    static class User {
        int id;
        String name;

        public User(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + "]";
        }

    }

    public static void main(String[] args) {
        System.out.println("-------- start --------");
        // new SubListOperation().outputString();
        new SubListOperation().outputObject();
        System.out.println("-------- finish --------");
    }

    public void outputString() {
        int pageNum = 2;
        int pageSize = 5;
        for (String s : subList(pageNum, pageSize)) {
            System.out.println(s);
        }
    }

    public void outputObject() {
        int pageNum = 2;
        int pageSize = 5;
        for (User s : userSubList(pageNum, pageSize)) {
            System.out.println(s);
        }
    }

}


-------- start --------
User [id=6, name=user6]
User [id=7, name=user7]
User [id=8, name=user8]
User [id=9, name=user9]
User [id=10, name=user10]
-------- finish --------

 

泛型方法,通用类型实现分页

package org.sun.java;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Administrator
 * @date 2016年7月16日
 */
public class SubListOperation {
    final static int COUNT = 18;

    public List<String> init(int size) {
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < size; i++) {
            lists.add("t" + (i + 1));
        }
        return lists;
    }

    /**
     * 
     * @param size
     * @return 初始化用户列表
     */
    public List<User> initUser(int size) {
        List<User> lists = new ArrayList<User>();
        for (int i = 0; i < size; i++) {
            lists.add(new User((i + 1), "user" + (i + 1)));
        }
        return lists;
    }

    

    /**
     * 
     * @param lists 将被分页展示的列表
     * @param pageNum 页码数
     * @param pageSize 每页条数大小
     * @return
     */
    public <T> List<T> generalPagenation(List<T> lists,int pageNum, int pageSize) {
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        int size = lists.size();
        if (fromIndex >= size) {
            return new ArrayList<T>();
        }
        return lists.subList(fromIndex, Math.min(size, toIndex));
    }
    

    static class User {
        int id;
        String name;

        public User(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + "]";
        }

    }

    public static void main(String[] args) {
        System.out.println("-------- start --------");
        new SubListOperation().outputT();
        System.out.println("-------- finish --------");
    }

    public void outputT() {
        int pageNum = 3;
        int pageSize = 5;
        for (Object s : generalPagenation(init(18),pageNum, pageSize)) {
            System.out.println(s);
        }
        System.out.println("---------***---------");
        for (Object s : generalPagenation(initUser(12),pageNum, pageSize)) {
            System.out.println(s);
        }
    }


}

-------- start --------
t11
t12
t13
t14
t15
---------***---------
User [id=11, name=user11]
User [id=12, name=user12]
-------- finish --------

 

 

 

posted @ 2016-07-16 01:35  Sea_Sky  阅读(566)  评论(0编辑  收藏  举报

转载请注明出处! About me