15

I have List of Object. I need to do pagination.
The input parameters are the maximum number object per Page and Page number.

For example input list = ("a", "b", "c", "d", "e", "f")
The maximum number per Page is 2 Page number is 2 Result = ("c", "d")

Are there any ready-made classes(libs) to do this? For example Apache project or so on.

0

8 Answers 8

24
int sizePerPage=2;
int page=2;

int from = Math.max(0,page*sizePerPage);
int to = Math.min(list.size(),(page+1)*sizePerPage)

list.subList(from,to)
Sign up to request clarification or add additional context in comments.

3 Comments

It's cleaner to use (page+1)*sizePerPage instead of page*sizePerPage+sizePerPage.
Two notices: 1) IndexOutOfBoundsException is not catched 2) This is 0 based page, not 1 like asked in question
if page is too large, from will be out of bound.
24

With Java 8 steams:

list.stream()
  .skip(page * size)
  .limit(size)
  .collect(Collectors.toCollection(ArrayList::new));

Comments

3

Try with:

int page    = 1; // starts with 0, so we on the 2nd page
int perPage = 2;

String[] list    = new String[] {"a", "b", "c", "d", "e", "f"};
String[] subList = null;

int size = list.length;
int from = page * perPage;
int to   = (page + 1) * perPage;
    to   = to < size ? to : size;

if ( from < size ) {
    subList = Arrays.copyOfRange(list, from, to);
}

1 Comment

This may produce a ArrayIndexOutOfBoundsException
3

Simple method

  public static <T> List<T> paginate(Page page, List<T> list) {
      int fromIndex = (page.getNumPage() - 1) * page.getLenght();
      int toIndex = fromIndex + page.getLenght();

      if (toIndex > list.size()) {
        toIndex = list.size();
      }

      if (fromIndex > toIndex) {
        fromIndex = toIndex;
      }

      return list.subList(fromIndex, toIndex);
  }

Comments

1

Try this:

int pagesize = 2;
int currentpage = 2;
list.subList(pagesize*(currentpage-1), pagesize*currentpage);

This code returns a list with only the elements you want (a page).

You should check the index also to avoid java.lang.IndexOutOfBoundsException.

Comments

1

Uses lombok for getters and setters

@Getter
@Setter
public class Pagination<T> {
  
  private int pageSize = 10;
  private int paginationSize = 9;
  private int currentPage = 0;
  private List<T> list = null;

  public Pagination(List<T> list) {
    this.list = list;
  }
  
  public int getSize() {
    return list == null ? 0 : list.size();
  }
  
  public int getOffset() {
    return getPageSize() * getCurrentPage();
  }
  
  public int getLimit() {
    return getPageSize();
  }
  
  public List<T> getItemsOnCurrentPage() {
    return list.subList(getOffset(), getOffset()+getLimit());
  }
  
  public int getPageCount() {
    return getSize() / getPageSize() + (getSize() % getPageSize() > 0 ? 1 : 0);
  }
  
  public int getActualPaginationSize() {
    return Math.min(getPaginationSize(), getPageCount());
  }
  
  public int getLastPage() {
    return getPageCount() - 1;
  }
  
  public int getStartPage() {
    if (getPageCount() > getPaginationSize()
      && getCurrentPage() > getPaginationSize() / 2) {
      return Math.min(getCurrentPage() - getPaginationSize() / 2,
          getPageCount() - getActualPaginationSize());
    } else {
      return 0;
    }
  }
  
  public int getEndPage() {
    return Math.min(getStartPage() + getPaginationSize(), getPageCount()) - 1;
  }
  
  public void setCurrentPage(int newPage) {
    if (newPage < 0) newPage = 0;
    if (newPage > getLastPage()) newPage = getLastPage();
    this.currentPage = newPage;
  }
  
  public boolean isHasPreviousPage() {
    return hasPreviousPage();
  }
  
  public boolean hasPreviousPage() {
    return getStartPage() > 0;
  }
  
  public boolean isHasNextPage() {
    return hasNextPage();
  }
  
  public boolean hasNextPage() {
    return getEndPage() < getLastPage();
  }
  
  public boolean isShouldShowFirst() {
    return shouldShowFirst();
  }
  
  public boolean shouldShowFirst() {
    return getStartPage() - getPaginationSize() / 2 > 0;
  }
  
  public boolean isShouldShowLast() {
    return shouldShowLast();
  }
  
  public boolean shouldShowLast() {
    return getEndPage() + getPaginationSize() / 2 < getLastPage();
  }
  
}

Page index start at 0, and current page is centered.

Comments

0

As per your question simple List.subList will give you expected behaviour size()/ 2= number of pages

Comments

0

You could use List.subList using Math.min to guard against ArrayIndexOutOfBoundsException :

List<String> list = Arrays.asList("a", "b", "c", "d", "e");
int pageSize = 2;
for (int i=0; i < list.size(); i += pageSize) {
    System.out.println(list.subList(i, Math.min(list.size(), i + pageSize)));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.