java集合分割
java集合分割成等份的小集合:
private <T> List<List<T>> getSubList(List list,int len) { if(list.size()<len){ return list; } List<List<T>> listGroup = new ArrayList<List<T>>(); int listSize = list.size(); //子集合的长度 int toIndex = len; for (int i = 0; i < list.size(); i += len) { if (i + len > listSize) { toIndex = listSize - i; } List<T> newList = list.subList(i, i + toIndex); listGroup.add(newList); } return listGroup; }
eg:
/** * @return * @Author * @Description //TODO 集合分割 * @Date 2019/1/24 16:48 * @Param */ private List<List<PushResult>> getSubList(List list, int len) { List<List<PushResult>> listGroup = new ArrayList<List<PushResult>>(); if (list.size() < len) { listGroup.add(list); return listGroup; } int listSize = list.size(); //子集合的长度 int toIndex = len; for (int i = 0; i < list.size(); i += len) { if (i + len > listSize) { toIndex = listSize - i; } List<PushResult> newList = list.subList(i, i + toIndex); listGroup.add(newList); } return listGroup; }