/**
* 按照条数对List进行分组
*
* @param list 目标集合
* @param toIndex 多少条为一组
* @return
*/
public static <T> Map<String, List<T>> groupList(List<T> list, int toIndex) {
if (list == null || list.isEmpty() || toIndex < 1) {
return new HashMap<>();
}
Map<String, List<T>> result = new HashMap<>();
int size = list.size();
int count = (size + toIndex - 1) / toIndex;
for (int i = 0; i < count; i++) {
List<T> subList = list.subList(i * toIndex, ((i + 1) * toIndex > size ? size : toIndex * (i + 1)));
result.put(i * toIndex + "", subList);
}
return result;
}
按照条数对List进行分组
最新推荐文章于 2024-09-02 16:13:34 发布