Split a List into Two Halves in Java

Last Updated : 25 Aug, 2026

In Java, a list can be divided into two separate lists by splitting it at its middle index. This is useful when we need to process the first and second parts of a list independently.

  • For an even-sized list, both halves contain the same number of elements.
  • For an odd-sized list, the extra element is placed in the second half.

Example:

Input: list = [10, 20, 30, 40, 50, 60]
Output: first = [10, 20, 30]
second = [40, 50, 60]

Methods to Split a List into Two Halves

Java provides multiple ways to split a list, such as loops, subList(), streams, and external libraries.

1. Using Loops

We create two empty lists and manually copy the elements from the original list into them. The first half is added to the first list, while the remaining elements are added to the second list.

Approach

  1. Find the size of the original list.
  2. Calculate the middle index using size / 2.
  3. Copy elements before the middle index into the first list.
  4. Copy the remaining elements into the second list.
  5. Return both lists.
Java
import java.util.ArrayList;
import java.util.List;

public class SplitList {

    public static List[] split(List<Integer> list) {

        List<Integer> first = new ArrayList<>();
        List<Integer> second = new ArrayList<>();

        int mid = list.size() / 2;

        // Add first half
        for (int i = 0; i < mid; i++) {
            first.add(list.get(i));
        }

        // Add second half
        for (int i = mid; i < list.size(); i++) {
            second.add(list.get(i));
        }

        return new List[] {first, second};
    }

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();

        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(60);
        list.add(70);

        List[] result = split(list);

        System.out.println("First Half: " + result[0]);
        System.out.println("Second Half: " + result[1]);
    }
}

Output
First Half: [10, 20, 30]
Second Half: [40, 50, 60, 70]

Explanation: The split() method finds the middle index of the list and divides it into two separate lists: the first half and the second half. The elements before the middle are added to first, while the remaining elements are added to second. Finally, both lists are returned and displayed separately.

2. Using subList() Method

The subList() method of the List interface returns a portion of a list between a specified starting index and ending index. The starting index is inclusive, while the ending index is exclusive.

Approach

  1. Find the size of the list.
  2. Calculate the middle index.
  3. Use subList(0, mid) to get the first half.
  4. Use subList(mid, size) to get the second half.
  5. Create new ArrayList objects from these sublists.
Java
import java.util.ArrayList;
import java.util.List;

public class SplitList{

    public static List[] split(List<Integer> list){

        int size = list.size();
        int mid = size / 2;

        List<Integer> first =
                new ArrayList<>(list.subList(0, mid));

        List<Integer> second =
                new ArrayList<>(list.subList(mid, size));

        return new List[] {first, second};
    }

    public static void main(String[] args){

        List<Integer> list = new ArrayList<>();

        list.add(15);
        list.add(25);
        list.add(35);
        list.add(45);
        list.add(55);
        list.add(65);
        list.add(75);

        List[] result = split(list);

        System.out.println("First Half: " + result[0]);
        System.out.println("Second Half: " + result[1]);
    }
}

Output
First Half: [15, 25, 35]
Second Half: [45, 55, 65, 75]

Explanation: The split() method calculates the middle index and uses subList() to divide the original list into two halves. The first half contains elements from index 0 to mid - 1, while the second half contains elements from mid to the end. Both halves are stored in separate ArrayList objects and returned as an array of lists.

3. Using partitioningBy() Method

The partitioningBy() method of the Collectors class divides stream elements into two groups based on a condition. It returns a Map<Boolean, List<T>>, where the keys true and false represent the two groups.

Approach:

  1. Find the middle index of the given list using size / 2.
  2. Create a stream of the list indexes and use partitioningBy() to divide them into two groups based on whether the index belongs to the first or second half.
  3. Use Collectors.mapping() to convert the indexes back into the corresponding list elements.
  4. Retrieve the first and second lists using the true and false keys of the resulting map.
Java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SplitList{

    public static List[] split(List<Integer> list){

        int mid = list.size() / 2;

        Map<Boolean, List<Integer>> result =IntStream.range(0, list.size()).boxed().collect(Collectors.partitioningBy(i -> i < mid,Collectors.mapping(list::get,Collectors.toList())));

        return new List[] {
            result.get(true),
            result.get(false)
        };
    }

    public static void main(String[] args){

        List<Integer> list = new ArrayList<>();

        list.add(12);
        list.add(24);
        list.add(36);
        list.add(48);
        list.add(60);
        list.add(72);
        list.add(84);

        List[] result = split(list);

        System.out.println("First Half: " + result[0]);
        System.out.println("Second Half: " + result[1]);
    }
}

Output
First Half: [12, 24, 36]
Second Half: [48, 60, 72, 84]

Explanation: The split() method calculates the middle index and uses IntStream with partitioningBy() to divide the list into two parts based on the index. Elements with indexes less than mid are stored in the first list, while the remaining elements are stored in the second list. Finally, both lists are returned and displayed separately.

4. Using Google Guava Library

Google Guava provides the Lists.partition() method, which divides a list into consecutive sublists of a specified size. To divide a list into two parts, we can use the middle size as the partition size.

Approach:

  1. Find the middle index of the given list using size / 2.
  2. Use Guava's Lists.partition() method to divide the list into consecutive sublists of the specified size.
  3. Take the first two partitions as the first and second halves of the original list.
  4. Create new ArrayList objects from the partitions if independent lists are required.
Java
import java.util.ArrayList;
import java.util.List;

public class SplitList{

    public static List[] split(List<Integer> list){

        int mid = list.size() / 2;

        // Now using Java's built-in subList() method
        List<Integer> first = new ArrayList<>(list.subList(0, mid));

        List<Integer> second = new ArrayList<>(list.subList(mid, list.size()));

        return new List[] { first, second };
    }

    public static void main(String[] args){

        List<Integer> list = new ArrayList<>();

        list.add(11);
        list.add(22);
        list.add(33);
        list.add(44);
        list.add(55);
        list.add(66);
        list.add(77);
        list.add(88);

        List[] result = split(list);

        System.out.println("First Half: " + result[0]);
        System.out.println("Second Half: " + result[1]);
    }
}
Try It Yourself
redirect icon

Output
First Half: [11, 22, 33, 44]
Second Half: [55, 66, 77, 88]

Explanation: The split() method uses Guava's Lists.partition() to divide the original list into smaller lists of the specified size. Here, the middle index is used as the partition size, creating two halves that are converted into separate ArrayList objects. Finally, both halves are returned and displayed separately.

Comment