What is Collectors.partitioningBy() in Java?

The Collectors class

Collectors is a utility class that provides various implementations of reduction operations such as grouping, collecting, and summarizing elements.

The different functionalities in the Collectors class are used as final operations on streams.

The partitioningBy() method

partitioningBy() is a static method of the Collectors class that is used to return a Collector that partitions the input stream of elements according to the given predicate.

There are two variations of the partitioningBy() method:

  1. partitioningBy(Predicate<? super T> predicate)
  2. partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

The partitioningBy method is defined in the Collectors class, which is defined in the java.util.stream package.

To import the Collectors class, we use the following import statement:

import java.util.stream.Collectors;

partitioningBy(Predicate<? super T> predicate)

This method is used to partition a stream of elements according to the given predicate and organize the result into a Map.

Syntax

public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)

Parameters

  • Predicate<? super T> predicate: The predicate that decides the partition to which the element belongs.

Return value

This method returns a Map.

Code

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// create an Integer stream
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Partitioning condition
Predicate<Integer> predicate = num -> num <= 5;
// Apply the partitioningBy to partition the stream of numbers into two halves
Map<Boolean, List<Integer>> map = integerStream.collect(Collectors.partitioningBy(predicate));
// print the result
System.out.println("Result of the partitioningBy method - " + map);
}
}

partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

This method returns a Collector that divides the input elements into partitions based on a predicate. partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream) uses another collector to reduce the values in each partition and organize them into a Map<Boolean, D> whose values are the result of the downstream reduction.

Examples of the downstream reduction can be the number, sum, etc., of the elements in each partition.

Syntax

public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)

Parameters

  • Predicate<? super T> predicate: The predicate that decides the partition to which the element belongs.
  • Collector<? super T, A, D> downstream: The Collector that implements the downstream reduction operation.

Return value

This method returns a Map.

Code

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args)
{
// create an Integer stream
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
// Partitioning condition to check whether the number is even
Predicate<Integer> predicate = num -> num % 2 == 0;
// Apply the partitioningBy to partition the stream of numbers into two halves
Map<Boolean, List<Integer>> map = integerStream.collect(Collectors.partitioningBy(predicate));
// print the result
System.out.println("Result after the partitioning - " + map);
// Recreate the stream
integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
// Apply the partitioningBy to partition the stream of numbers into two partitions and count the number of elements in each partition
Map<Boolean, Long> booleanIntegerMap = integerStream.collect(Collectors.partitioningBy(predicate, Collectors.counting()));
// print the result
System.out.println("Result of partitioning and reducing - " + booleanIntegerMap);
}
}

Free Resources