Collectors partitioningBy() method in Java
Last Updated :
06 Dec, 2018
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:
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Gfg {
public static void main(String[] args)
{
Stream<Integer>
s = Stream.of( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
Map<Boolean, List<Integer> >
map = s.collect(
Collectors.partitioningBy(num -> num > 3 ));
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:
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ArraytoArrayList {
public static void main(String[] args)
{
Stream<Integer>
s = Stream.of( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
Map<Boolean, Long>
map = s.collect(
Collectors.partitioningBy(
num -> (num > 3 ), Collectors.counting()));
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
Share your thoughts in the comments
Please Login to comment...