Fundamentals 9 min read

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.

macrozheng
macrozheng
macrozheng
How to Efficiently Split Large Java Lists into Smaller Chunks

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>&lt;dependency&gt;
  &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
  &lt;artifactId&gt;guava&lt;/artifactId&gt;
  &lt;version&gt;31.0.1-jre&lt;/version&gt;
&lt;/dependency&gt;</code>

Use

Lists.partition

to 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>&lt;dependency&gt;
  &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
  &lt;artifactId&gt;commons-collections4&lt;/artifactId&gt;
  &lt;version&gt;4.4&lt;/version&gt;
&lt;/dependency&gt;</code>

Use

ListUtils.partition

to 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>&lt;dependency&gt;
  &lt;groupId&gt;cn.hutool&lt;/groupId&gt;
  &lt;artifactId&gt;hutool-all&lt;/artifactId&gt;
  &lt;version&gt;5.7.14&lt;/version&gt;
&lt;/dependency&gt;</code>

Use

ListUtil.partition

to 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.subList

directly:

<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

subList

method can be used.

JavaGuavaHutoolStreamApache CommonsList PartitionsubList
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.