partitioningBy
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)
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.
Example
public void partitioningByPredicate() {
List<String> names = Arrays.asList("zjc", "stream", "collector", "map", "mapping");
Map<Boolean, List<String>> partitionByNameLength = names.stream()
.collect(Collectors.partitioningBy(name -> name.length() > 3));
System.out.println(partitionByNameLength);
}
output
{false=[zjc, map], true=[stream, collector, mapping]}
partitioningBy(predicate, collector)
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.
Example
public void partitioningByPredicateDownstream() {
List<String> names = Arrays.asList("zjc", "stream", "collector", "map", "mapping");
Map<Boolean, List<String>> partitionByNameLength = names.stream()
.collect(Collectors.partitioningBy(name -> name.length() > 3,
Collectors.mapping(String::toUpperCase, Collectors.toList())));
System.out.println(partitionByNameLength);
}
output
{false=[zjc, map], true=[stream, collector, mapping]}
Example
public void partitioningByPredicateCounting() {
List<Double> dbs = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
Map<Boolean, Long> partitionByAvgGrade = dbs.stream()
.collect(Collectors.partitioningBy(db-> db > 3.0,
Collectors.counting()));
System.out.println(partitionByAvgGrade);
}
output
{false=3, true=2}