Always wanted to use reliable Java code for generic pagination. Finally I found spring-data-commons by @olivergierke. Exactly what I was looking for. I made a quick reminder for myself on this matter:
Maven dependency
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.13.13.RELEASE</version> </dependency>
Imports
import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable;
Page 1/3 Example
public static void main(String[] args) { int pageNumber = 0; int pageSize = 5; Pageable pageable = new PageRequest(pageNumber, pageSize); String[] data = { "1", "2", "3", "4", "5" }; List<String> elements = Arrays.asList(data); Page<String> page = new PageImpl<>(elements, pageable, 12); System.out.println("Page number: " + page.getNumber()); System.out.println("Page size: " + page.getSize()); System.out.println("Page content: " + page.getContent()); System.out.println(); System.out.println("Number of elements in this page: " + page.getNumberOfElements()); System.out.println("Total elements: " + page.getTotalElements()); System.out.println("Total pages: " + page.getTotalPages()); System.out.println("Is first: " + page.isFirst()); System.out.println("Is last: " + page.isLast()); System.out.println(); System.out.println("Previous pageable: " + page.previousPageable()); System.out.println("Next pageable: " + page.nextPageable()); }
Page 2/3 Example
public static void main(String[] args) { int pageNumber = 1; int pageSize = 5; Pageable pageable = new PageRequest(pageNumber, pageSize); String[] data = {"6", "7", "8", "9", "10" }; List<String> elements = Arrays.asList(data); Page<String> page = new PageImpl<>(elements, pageable, 12); System.out.println("Page number: " + page.getNumber()); System.out.println("Page size: " + page.getSize()); System.out.println("Page content: " + page.getContent()); System.out.println(); System.out.println("Number of elements in this page: " + page.getNumberOfElements()); System.out.println("Total elements: " + page.getTotalElements()); System.out.println("Total pages: " + page.getTotalPages()); System.out.println("Is first: " + page.isFirst()); System.out.println("Is last: " + page.isLast()); System.out.println(); System.out.println("Previous pageable: " + page.previousPageable()); System.out.println("Next pageable: " + page.nextPageable()); }
Page 3/3 Example
public static void main(String[] args) { int pageNumber = 2; int pageSize = 5; Pageable pageable = new PageRequest(pageNumber, pageSize); String[] data = { "11", "12" }; List<String> elements = Arrays.asList(data); Page<String> page = new PageImpl<>(elements, pageable, 12); System.out.println("Page number: " + page.getNumber()); System.out.println("Page size: " + page.getSize()); System.out.println("Page content: " + page.getContent()); System.out.println(); System.out.println("Number of elements in this page: " + page.getNumberOfElements()); System.out.println("Total elements: " + page.getTotalElements()); System.out.println("Total pages: " + page.getTotalPages()); System.out.println("Is first: " + page.isFirst()); System.out.println("Is last: " + page.isLast()); System.out.println(); System.out.println("Previous pageable: " + page.previousPageable()); System.out.println("Next pageable: " + page.nextPageable()); }