How to Efficiently Split Large Java Lists into Smaller Chunks
This article explains why MySQL’s SQL length limit can cause batch‑insert failures and demonstrates five practical ways to partition a large Java List—using Guava, Apache Commons, Hutool, JDK Stream, and custom subList—complete with code examples and execution results.
When performing MyBatis batch inserts, a very large List can generate an excessively long SQL statement that exceeds MySQL’s maximum allowed length, causing execution errors.
To avoid this, the list can be split into smaller sub‑lists. The most reliable approach is to partition the List rather than trying to increase MySQL’s SQL length limit.
Introduction
Splitting a List into multiple smaller Lists is called "partitioning" (or "list splitting").
Common Java implementations include:
Google Guava
Apache Commons Collections
Hutool
JDK 8 Stream API
Custom subList logic
Below are detailed examples for each method.
1. Google Guava
Add the Guava dependency to
pom.xml:
<code><dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency></code>Use
Lists.partitionto split the list:
<code>import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
/** Guava partition example */
public class PartitionByGuavaExample {
private static final List<String> OLD_LIST = Arrays.asList(
"唐僧,悟空,八戒,沙僧,曹操,刘备,孙权".split(","));
public static void main(String[] args) {
List<List<String>> newList = Lists.partition(OLD_LIST, 3);
newList.forEach(i -> System.out.println("集合长度:" + i.size()));
}
}
</code>Execution result:
2. Apache Commons Collections
Add the Commons Collections dependency:
<code><dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency></code>Use
ListUtils.partitionto split the list:
<code>import org.apache.commons.collections4.ListUtils;
import java.util.Arrays;
import java.util.List;
/** Commons Collections partition example */
public class PartitionExample {
private static final List<String> OLD_LIST = Arrays.asList(
"唐僧,悟空,八戒,沙僧,曹操,刘备,孙权".split(","));
public static void main(String[] args) {
List<List<String>> newList = ListUtils.partition(OLD_LIST, 3);
newList.forEach(i -> System.out.println("集合长度:" + i.size()));
}
}
</code>Execution result:
3. Hutool
Add the Hutool dependency:
<code><dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency></code>Use
ListUtil.partitionto split the list:
<code>import cn.hutool.core.collection.ListUtil;
import java.util.Arrays;
import java.util.List;
public class PartitionByHutoolExample {
private static final List<String> OLD_LIST = Arrays.asList(
"唐僧,悟空,八戒,沙僧,曹操,刘备,孙权".split(","));
public static void main(String[] args) {
List<List<String>> newList = ListUtil.partition(OLD_LIST, 3);
newList.forEach(i -> System.out.println("集合长度:" + i.size()));
}
}
</code>Execution result:
4. JDK 8 Stream
Using the Stream API requires no additional libraries:
<code>import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** JDK Stream partition example */
public class PartitionByStreamExample {
private static final List<Integer> OLD_LIST = Arrays.asList(1,2,3,4,5,6);
public static void main(String[] args) {
Map<Boolean, List<Integer>> newMap = OLD_LIST.stream()
.collect(Collectors.partitioningBy(i -> i > 3));
System.out.println(newMap);
}
}
</code>Result shows two groups: numbers greater than 3 and those less or equal.
This method is simple but only supports binary partitioning based on a clear condition.
5. Custom SubList
If third‑party libraries are not desired and Stream partitioning is insufficient, you can use
List.subListdirectly:
<code>import java.util.Arrays;
import java.util.List;
public class App {
private static final List<String> _OLD_LIST = Arrays.asList(
"唐僧,悟空,八戒,沙僧,曹操,刘备,孙权".split(","));
public static void main(String[] args) {
List<String> list = _OLD_LIST.subList(0, 3);
list.forEach(i -> System.out.println(i));
}
}
</code>Execution result:
Summary
The article presents five ways to partition a Java List. Introducing a third‑party library such as Guava, Apache Commons, or Hutool offers the most convenient solution. For simple cases, the JDK Stream API or the built‑in
subListmethod can be used.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.