Verwenden Sie org.apache.commons.collections4.ListUtils # partition ().
Javadoc https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#partition-java.util.List-int-
Maven https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.4
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
Geben Sie die maximale Anzahl von Elementen pro Element an
List<String> list0 = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");
List<List<String>> lists = ListUtils.partition(list0, 3);
for (List<String> list : lists) {
System.err.println(list);
}
[aaa, bbb, ccc]
[ddd, eee, fff]
[ggg]
Geben Sie an, wie viele Sie teilen möchten
List<String> list0 = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg");
List<List<String>> lists = ListUtils.partition(list0, list0.size() / 2 + 1);
for (List<String> ll : lists) {
System.err.println(ll);
}
[aaa, bbb, ccc, ddd]
[eee, fff, ggg]
Ich habe eine ähnliche Methode selbst gemacht und abgeschlossen, aber mir wurde klar, dass "This is in Commons !?" Und ich kam zur Entdeckung orz.
das ist alles.