Java list paging is convenient for batch processing of the data of the whole list

Here is the programming house jb51 CC collects and arranges code fragments through the network.

Programming house Xiaobian now shares it with you and gives you a reference.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListPageUtil<T> {
	
    /**
     * 每页显示条数
     */
    private int pageSize;
    
    /**
     * 总页数
     */
    private int pageCount;
    
    /**
     * 原集合
     */
    private List<T> data;

    public ListPageUtil(List<T> data,int pageSize) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("data must be not empty!");
        }

        this.data = data;
        this.pageSize = pageSize;
        this.pageCount = data.size()/pageSize;
        if(data.size()%pageSize!=0){
        	this.pageCount++;
        }
    }

    /**
     * 得到分页后的数据
     *
     * @param pageNum 页码
     * @return 分页后结果
     */
    public List<T> getPagedList(int pageNum) {
        int fromIndex = (pageNum - 1) * pageSize;
        if (fromIndex >= data.size()) {
            return Collections.emptyList();
        }

        int toIndex = pageNum * pageSize;
        if (toIndex >= data.size()) {
            toIndex = data.size();
        }
        return data.subList(fromIndex,toIndex);
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<T> getData() {
        return data;
    }

    public int getPageCount() {
		return pageCount;
	}

	public static void main(String[] args) {
        Integer[] array = {1,2,3,4,5,6,7,8,9,10,11,12};
        List<Integer> list = Arrays.asList(array);

        ListPageUtil<Integer> pager = new ListPageUtil<Integer>(list,10);
       
        System.out.println(pager.getPageCount());

        List<Integer> page1 = pager.getPagedList(1);
        System.out.println(page1);

        List<Integer> page2 = pager.getPagedList(2);
        System.out.println(page2);

        List<Integer> page3 = pager.getPagedList(3);
        System.out.println(page3);
    }
}

The above is all the code content collected by the programming home (jb51. CC). I hope this article can help you solve the program development problems you encounter.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>