Open In App

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,



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,

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}

Article Tags :