get Sub List By Page number and page size - Java java.util

Java examples for java.util:List Sub List

Description

get Sub List By Page number and page size

Demo Code


//package com.java2s;

import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        int pageNumber = 42;
        int pageSize = 42;
        System.out.println(getSubListByPage(list, pageNumber, pageSize));
    }/*ww w  .j  av a  2 s . co  m*/

    public static List<?> getSubListByPage(List<?> list, int pageNumber,
            int pageSize) {
        int start = 0;
        int end = 0;

        if (pageNumber >= 0) {
            start = pageSize * (pageNumber - 1);

        }
        if (pageSize > 0) {
            end = start + pageSize;
        } else {
            end = start;
        }
        if (list.size() < end + 1) {
            end = list.size() - 1;
        }
        end = end < 0 ? 0 : end;
        List<?> subList = list.subList(start, end);
        return subList;

    }
}

Related Tutorials