Collectors partitioningBy() method is a predefined method of java.util.stream.Collectors class which is used to partition a stream of objects(or a set of elements) based on a given predicate. There are two overloaded variants of the method that are present. One takes only a predicate as a parameter whereas the other takes both predicate and a collector instance as parameters.
partitioningBy(Predicate<? super T> predicate)
Syntax:
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
where,
- Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
- T: The type of input elements to the reduction operation.
- A: The mutable accumulation type of the reduction operation.
- R: The result type of the reduction operation.
- Map<Boolean, List<T>>: The map containing the output.Keys are boolean values(true or false) and the corresponding values are lists containing elements of type T.
Parameters: This method takes a mandatory parameter predicate which an instance of a Predicate Interface of type T.
Return Value: This method returns a Collector implementing the partitioning operation.
Below is an example to illustrate partitioningBy() method:
Program:
// Java code to show the implementation of // Collectors partitioningBy() function import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; class Gfg { // Driver code public static void main(String[] args) { // creating an Integer stream Stream<Integer> s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // using Collectors partitioningBy() // method to split the stream of elements into // 2 parts, greater than 3 and less than 3. Map<Boolean, List<Integer> > map = s.collect( Collectors.partitioningBy(num -> num > 3)); // Displaying the result as a map // true if greater than 3, false otherwise System.out.println("Elements in stream " + "partitioned by " + "less than equal to 3: \n" + map); } } |
Elements in stream partitioned by less than equal to 3:
{false=[1, 2, 3], true=[4, 5, 6, 7, 8, 9, 10]}
partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)
Syntax:
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)
where,
- Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
- T: The type of input elements to the reduction operation.
- A: The mutable accumulation type of the reduction operation.
- R: The result type of the reduction operation.
- Map<Boolean, List<T>>: The map containing the output.Keys are boolean values(true or false) and the corresponding values are lists containing elements of type T.
Parameters: This method takes two parameters a predicate which an instance of a Predicate Interface of type T, and a collector which is used for implementing “downstream reduction” and producing the output.
Return Value: This method returns a Collector implementing the partitioning operation.
Below is an example to illustrate type 2 of partitioningBy() method:
// Java code to show the implementation of // Collectors partitioningBy() function import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; class ArraytoArrayList { // Driver code public static void main(String[] args) { // creating an Integer stream Stream<Integer> s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Using Collectors.counting() method // to count the number of elements in // the 2 partitions Map<Boolean, Long> map = s.collect( Collectors.partitioningBy( num -> (num > 3), Collectors.counting())); // Displaying the result as a map // true if greater than 3, false otherwise System.out.println("Elements in stream " + "partitioned by " + "less than equal to 3: \n" + map); } } |
Elements in stream partitioned by less than equal to 3:
{false=3, true=7}
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.
Recommended Posts:
- Java Stream | Collectors toCollection() in Java
- Java 8 Streams | Collectors.joining() method with Examples
- Collectors toList() method in Java with Examples
- Collectors toMap() method in Java with Examples
- Collectors collectingAndThen() method in Java with Examples
- Collectors groupingBy() method in Java with Examples
- Java 8 | Collectors counting() with Examples
- Java | Collectors averagingDouble() with Examples
- Java | Collectors averagingLong (ToLongFunction mapper) with Examples
- Java 8 | Collectors averagingInt() with Examples
- Java | Collectors maxBy(Comparator comparator) with Examples
- Java | Collectors minBy(Comparator comparator) with Examples
- Collectors toSet() in Java with Examples
- Java Collectors
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.BitSet.set() method in Java
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.

