Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to make pagination at List in java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Reference: https://stackoverflow.com/questions/19688235/how-to-implement-pagination-on-a-list
* @author elvis
* @version $Revision: $<br/>
* $Id: $
* @since 6/21/17 7:28 PM
*/
public class ListUtils {
public static <T> List<T> getPage(List<T> sourceList, int page, int pageSize) {
if(pageSize <= 0 || page <= 0) {
throw new IllegalArgumentException("invalid page size: " + pageSize);
}
int fromIndex = (page - 1) * pageSize;
if(sourceList == null || sourceList.size() < fromIndex){
return Collections.emptyList();
}
// toIndex exclusive
return sourceList.subList(fromIndex, Math.min(fromIndex + pageSize, sourceList.size()));
}
public static <T> void doPaginated(Collection<T> fullList, Integer pageSize, Page<T> pageInterface) {
final List<T> list = new ArrayList<T>(fullList);
if (pageSize == null || pageSize <= 0 || pageSize > list.size()){
pageSize = list.size();
}
final int numPages = (int) Math.ceil((double)list.size() / (double)pageSize);
for (int pageNum = 0; pageNum < numPages;){
final List<T> page = list.subList(pageNum * pageSize, Math.min(++pageNum * pageSize, list.size()));
pageInterface.run(page);
}
}
public interface Page<T> {
void run(List<T> item);
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
/**
* @author elvis
* @version $Revision: $<br/>
* $Id: $
* @since 6/21/17 7:35 PM
*/
public class ListUtilsTest {
@Test
public void doPaginated_InTwoPages() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 4;
final List<List<Integer>> results = new ArrayList<>();
ListUtils.doPaginated(numbers, pageSize, x -> {
results.add(x);
});
Assert.assertEquals("[[1, 2, 3, 4], [5, 6]]", results.toString());
}
@Test
public void doPaginated_PerfectDivision() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 2;
final List<List<Integer>> results = new ArrayList<>();
ListUtils.doPaginated(numbers, pageSize, x -> {
results.add(x);
});
Assert.assertEquals("[[1, 2], [3, 4], [5, 6]]", results.toString());
}
@Test
public void doPaginated_EmptyList() throws Exception {
final List<Integer> numbers = Arrays.asList();
final int pageSize = 2;
final List<List<Integer>> results = new ArrayList<>();
ListUtils.doPaginated(numbers, pageSize, x -> {
results.add(x);
});
Assert.assertEquals("[]", results.toString());
}
@Test
public void doPaginated_PageSizeGreaterThanList() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2);
final int pageSize = 3;
final List<List<Integer>> results = new ArrayList<>();
ListUtils.doPaginated(numbers, pageSize, x -> {
results.add(x);
});
Assert.assertEquals("[[1, 2]]", results.toString());
}
@Test
public void doPaginated_PageSizeEqualToList() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2);
final int pageSize = 2;
final List<List<Integer>> results = new ArrayList<>();
ListUtils.doPaginated(numbers, pageSize, x -> {
results.add(x);
});
Assert.assertEquals("[[1, 2]]", results.toString());
}
@Test
public void testGetPage_PaginationInTwoPages() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 4;
int page = 1;
Assert.assertEquals("[1, 2, 3, 4]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[5, 6]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[]", ListUtils.getPage(numbers, page++, pageSize).toString());
}
@Test
public void testGetPage_PageSizeGreaterThanList() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 10;
int page = 1;
Assert.assertEquals("[1, 2, 3, 4, 5, 6]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[]", ListUtils.getPage(numbers, page++, pageSize).toString());
}
@Test
public void testGetPage_PageSizeEqualToList() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 6;
int page = 1;
Assert.assertEquals("[1, 2, 3, 4, 5, 6]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[]", ListUtils.getPage(numbers, page++, pageSize).toString());
}
@Test
public void testGetPage_PerfectDivision() throws Exception {
final List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
final int pageSize = 2;
int page = 1;
Assert.assertEquals("[1, 2]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[3, 4]", ListUtils.getPage(numbers, page++, pageSize).toString());
Assert.assertEquals("[5, 6]", ListUtils.getPage(numbers, page++, pageSize).toString());
}
@Test
public void testGetPage_EmptyList() throws Exception {
final List<Integer> numbers = Arrays.asList();
final int pageSize = 4;
int page = 1;
Assert.assertEquals("[]", ListUtils.getPage(numbers, page, pageSize).toString());
Assert.assertEquals("[]", ListUtils.getPage(numbers, ++page, pageSize).toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment