/** * 将一组数据固定分组,每组n个元素 * * @param source 要分组的数据源 * @param limit 每组n个元素 * @param <T> * @return */ public static <T> List<List<T>> fixedGroup(List<T> source, int limit) { if (null == source || source.size() == 0 || limit <= 0) return null; List<List<T>> result = new ArrayList<List<T>>(); int remainder = source.size() % limit; int size = (source.size() / limit); for (int i = 0; i < size; i++) { List<T> subset = null; subset = source.subList(i * limit, (i + 1) * limit); result.add(subset); } if (remainder > 0) { List<T> subset = null; subset = source.subList(size * limit, size * limit + remainder); result.add(subset); } return result; }