Open In App

Collectors partitioningBy() method in Java

Last Updated : 06 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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


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:




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


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads