Collectors partitioningBy() method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Elements in stream partitioned by less than equal to 3: 
{false=3, true=7}

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


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.