Conditionally split a List in Java
This post will discuss how to conditionally split a list in Java.
1. Using Collectors.partitioningBy()
method
The Collectors.partitioningBy()
method returns a collector which can partition the list according to a supplied predicate and organizes the list into a Map<Boolean, List<T>>
.
It can be used to split the list into two sublists, corresponding to whether the predicate was matched, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); Collection<List<Integer>> results = nums.stream() .collect(Collectors.partitioningBy(n -> n % 2 == 0)) .values(); System.out.println(results); } } |
Output:
[[1, 3, 5, 7], [2, 4, 6, 8]]
2. Using Collectors.groupingBy()
method
You can also use the Collectors.groupingBy()
method to apply a “group by” operation on the list elements. It groups the elements according to a classification function and returns the results in a map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); Collection<List<Integer>> results = nums.stream() .collect(Collectors.groupingBy(n -> n % 2 == 0)) .values(); System.out.println(results); } } |
Output:
[[1, 3, 5, 7], [2, 4, 6, 8]]
3. Using Stream.filter()
method
Another simple solution to split a list in Java involves filtering the list. This can be done using filters in the Java 8 Stream API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<Integer> odd = nums.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); List<Integer> even = nums.stream().filter(i -> i % 2 == 1).collect(Collectors.toList()); System.out.println(odd); System.out.println(even); } } |
Output:
[2, 4, 6, 8]
[1, 3, 5, 7]
This is equivalent to the following code (works with earlier versions of Java):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<Integer> odd = new ArrayList<>(); List<Integer> even = new ArrayList<>(); for (Integer num : nums) { if (num % 2 == 0) { odd.add(num); } else { even.add(num); } } System.out.println(odd); System.out.println(even); } } |
Output:
[2, 4, 6, 8]
[1, 3, 5, 7]
4. Using Collectors.teeing()
method
Since Java 12, it can be done using the Collectors.teeing() method. It returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by the downstream collectors, then their results are merged using the specified merge function into the final result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); List<List<Integer>> lists = nums.stream().collect( Collectors.teeing( Collectors.filtering(i -> i % 2 == 0, Collectors.toList()), Collectors.filtering(i -> i % 2 == 1, Collectors.toList()), List::of )); System.out.println(lists.get(0)); System.out.println(lists.get(1)); } } |
Output:
[2, 4, 6, 8]
That’s all about conditionally splitting a list in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)