Learn Elastic-Job - Distributed Scheduled Task Framework
When viewing the source code for multi-threaded sharding operations, the tool class uses the Google Guava package of the collection class to perform the grouping operation of the list.
//Equally divided into nested lists of size 2 for each sublist Lists.partition()
You can use this tool to group list operations
package com.dangdang.ddframe; import java.util.ArrayList; import java.util.List; import com.google.common.collect.Lists; public class Test { class Foo { private int id; private String name; public Foo(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Foo [id=" + id + ", name=" + name + "]"; } } public static void main(String[] args) { List<Foo> data = new ArrayList<Foo>(); for (int i = 0; i < 20; i++) { Test.Foo f = new Test().new Foo(i, i + "名称"); data.add(f); } List<List<Foo>> splitData = Lists.partition(data, data.size() / 10); System.out.println("Number of groups: " + data.size() / 10); System.out.println(splitData.size()); for (int i = 0; i < splitData.size(); i++) { System.out.println("分组:" + i); // List<Foo> s = splitData.get(i); System.out.println(splitData.get(i).toString()); } } }