# 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.

 `// 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 list) ` `    ``{ ` `        ``// create two empty lists ` `        ``List first = ``new` `ArrayList(); ` `        ``List second = ``new` `ArrayList(); ` ` `  `        ``// 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 list = ``new` `ArrayList(); ` ` `  `        ``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``]); ` `    ``} ` `} `

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.

 `// 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 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 first = ``new` `ArrayList<>(list.subList(``0``, (size) / ``2``)); ` `        ``List 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 list = ``new` `ArrayList(); ` ` `  `        ``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``]); ` `    ``} ` `} `

[/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)```

 `// 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 list) ` `    ``{ ` `        ``int` `midIndex = (((list.size()) / ``2``) - ``1``); ` ` `  `        ``List > 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 list = ``new` `ArrayList(); ` `        ``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``]); ` `    ``} ` `} `

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.

 `// 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 list) ` `    ``{ ` `        ``// partition the List into two sublists and get iterator ` `        ``Iterator > 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 list = ``new` `ArrayList(); ` `        ``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``]); ` `    ``} ` `} `

Output :

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.