Java examples for java.util:List Split
Divide List by page size
//package com.java2s; import java.util.Collections; import java.util.List; import java.util.Map; public class Main { public static void main(String[] argv) { List list = java.util.Arrays.asList("asdf", "java2s.com"); int page = 42; int size = 42; System.out.println(pageList(list, page, size)); }//from w ww .ja v a2s . c o m public static <T> List<T> pageList(List<T> list, int page, int size) { if (page < 1) { throw new IllegalArgumentException( "page should not little than 1,page:" + page); } if (size < 1) { throw new IllegalArgumentException( "size should not little than 1,size:" + size); } if (isEmpty(list)) { return Collections.emptyList(); } int totalSize = list.size(); int fromIndex = (page - 1) * size > (totalSize - 1) ? 0 : (page - 1) * size; int endIndex = (fromIndex + size) > (totalSize) ? (totalSize) : (fromIndex + size); return list.subList(fromIndex, endIndex); } public static <T> boolean isEmpty(List<T> list) { return list == null || list.isEmpty(); } public static <K, V> boolean isEmpty(Map<K, V> map) { return map == null || map.isEmpty(); } }