Split a list into two halves in Java

Given a list, we need to split into two news lists in Java.

Input : list = {1, 2, 3, 4, 5, 6}
Output : first = {1, 2, 3}, second = {4, 5, 6}

Input : list = {1, 2, 3, 4, 5}
Output : first = {1, 2}, second = {3, 4, 5}

Method 1 (Naive) First create two new empty lists and assign first half element of original list and rest into second empty list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to split a list into two sublist in Java
import java.util.ArrayList;
import java.util.List;
  
public class GFG {
  
    // function to split a list into two sublists in Java
    public static List[] split(List<String> list)
    {
        // create two empty lists
        List<String> first = new ArrayList<String>();
        List<String> second = new ArrayList<String>();
  
        // get size of the list
        int size = list.size();
  
        // First size)/2 element copy into list
        // first and rest second list
        for (int i = 0; i < size / 2; i++)
            first.add(list.get(i));
  
        // Second size)/2 element copy into list
        // first and rest second list
        for (int i = size / 2; i < size; i++)
            second.add(list.get(i));
  
        // return a List of array
        return new List[] { first, second };
    }
  
    public static void main(String[] args)
    {
        // create an ArrayList
        List<String> list = new ArrayList<String>();
  
        list.add("Geeks");
        list.add("Practice");
        list.add("Contribute");
        list.add("IDE");
        list.add("Courses");
  
        // call split method which return List of array
        List[] lists = split(list);
  
        System.out.println(lists[0]);
        System.out.println(lists[1]);
    }
}

chevron_right


Output :



[Geeks, Practice]
[Contribute, IDE, Courses]

Method 2 List.subList() Method
Returns a view of the portion of this list between the specified index(include) to other index(exclude) Ex. 2 to 5, Here index 2 will include only .In case if both specified index are equal, the returned list is empty. The List.subList() returned a list, so non-structural changes in the returned list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to split a list into two sub list in Java
import java.util.ArrayList;
import java.util.List;
  
public class GFG {
  
    // function to split a list into two sublists in Java
    public static List[] split(List<String> list)
    {
  
        // find size of the list and put in size
        int size = list.size();
  
        // create new list and insert valuese which is returne by
        // list.subList() method
        List<String> first = new ArrayList<>(list.subList(0, (size) / 2));
        List<String> second = new ArrayList<>(list.subList((size) / 2, size));
  
        // return an List of array
        return new List[] { first, second };
    }
  
    public static void main(String[] args)
    {
        // create an ArrayList
        List<String> list = new ArrayList<String>();
  
        list.add("Geeks");
        list.add("Practice");
        list.add("Contribute");
        list.add("IDE");
        list.add("Courses");
  
        // call split method which return List of array
        List[] lists = split(list);
  
        System.out.println(lists[0]);
        System.out.println(lists[1]);
    }
}

chevron_right


[/sourcecode]
Output :

[Geeks, Practice]
[Contribute, IDE, Courses]

Method 3 ( Using Collectors.partitioningBy() )
Java 8 Collectors.partitioningBy() is a method that partitions the elements of stream always in two part unlike Naive and List.subList(). It returns a Collector that store the values in a Map. Key of map can be only in Boolean. The syntax of partitioningBy method is

        public static  Collector<T, ?, Map<Boolean, List>> 
                                      partitioningBy(Predicate predicate)
filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to split a list into two sub list in Java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
  
public class GFG {
  
    // function to split a list into two sublists in Java
    public static List[] split(List<String> list)
    {
        int midIndex = (((list.size()) / 2) - 1);
  
        List<List<String> > lists = new ArrayList<>(
            list.stream()
                .collect(Collectors.partitioningBy(s -> list.indexOf(s) > midIndex))
                .values());
  
        // return an array containing both lists
        return new List[] { lists.get(0), lists.get(1) };
    }
  
    public static void main(String[] args)
    {
        // create an ArrayList
        List<String> list = new ArrayList<String>();
        list.add("Geeks");
        list.add("Practice");
        list.add("Contribute");
        list.add("IDE");
        list.add("Courses");
  
        // call split method which return List of array
        List[] lists = split(list);
  
        System.out.println(lists[0]);
        System.out.println(lists[1]);
    }
}

chevron_right


Output :

[Geeks, Practice]
[Contribute, IDE, Courses]

Method 4 (For Guava) Guava is an open source Java-based library which is developed by Google Inc. In Guava library, we can use Lists.partition() method that splits the list into consecutive sublists, every list specified size. In order to split the list into two sublists, In our case we can pass the size that is equal to half the size of our list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to split a list into two sub list in Java
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
  
public class GFG {
  
    // function to split a list into two sublists in Java
    public static List[] split(List<String> list)
    {
        // partition the List into two sublists and get iterator
        Iterator<List<String> > itr =
                  Iterables.partition(list, (list.size()) / 2)
                                          .iterator();
  
        // return an array containing both lists
        return new List[] { new ArrayList<>(itr.next()),
                            new ArrayList<>(itr.next()) };
    }
  
    public static void main(String[] args)
    {
        // create an ArrayList
        List<String> list = new ArrayList<String>();
        list.add("Geeks");
        list.add("Practice");
        list.add("Contribute");
        list.add("IDE");
        list.add("Courses");
  
        // call split method which return List of array
        List[] lists = split(list);
  
        System.out.println(lists[0]);
        System.out.println(lists[1]);
    }
}

chevron_right


Output :

[Geeks, Practice]
[Contribute, IDE, Courses]


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.