java将list拆分为多个list

  • Post author:
  • Post category:PHP
  • Post comments:0评论

方法封装

public static List<List<String>> groupList(int toIndex, List<String> list) {
List<List<String>> listGroup = new ArrayList<>();
int listSize = list.size();

for (int i = 0; i < list.size(); i += toIndex) {
if (i + toIndex > listSize) {
toIndex = listSize - i;
}
List<String> newList = list.subList(i, i + toIndex);
System.out.println(newList);
listGroup.add(newList);
}
return listGroup;
}

调用方法

public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 300; i++) {
list.add(i + "");
}
System.out.println("list:" + list);
List<List<String>> lists = groupList(7, list);
System.out.println("lists:" + lists);
}

Dylan

面朝大海·春暖花开

发表评论