Collectors classCollectors 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.
partitioningBy() methodpartitioningBy() 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:
partitioningBy(Predicate<? super T> predicate)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.
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
Predicate<? super T> predicate: The predicate that decides the partition to which the element belongs.This method returns a Map.
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 streamStream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);// Partitioning conditionPredicate<Integer> predicate = num -> num <= 5;// Apply the partitioningBy to partition the stream of numbers into two halvesMap<Boolean, List<Integer>> map = integerStream.collect(Collectors.partitioningBy(predicate));// print the resultSystem.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.
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
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.This method returns a Map.
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 streamStream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);// Partitioning condition to check whether the number is evenPredicate<Integer> predicate = num -> num % 2 == 0;// Apply the partitioningBy to partition the stream of numbers into two halvesMap<Boolean, List<Integer>> map = integerStream.collect(Collectors.partitioningBy(predicate));// print the resultSystem.out.println("Result after the partitioning - " + map);// Recreate the streamintegerStream = 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 partitionMap<Boolean, Long> booleanIntegerMap = integerStream.collect(Collectors.partitioningBy(predicate, Collectors.counting()));// print the resultSystem.out.println("Result of partitioning and reducing - " + booleanIntegerMap);}}