Java SE API
Collectors.partitioningBy()
Separates values into true and false lists; both keys exist even for an empty partition. Java SE API; example uses Java 21-compatible syntax.
Formal Syntax & Structure
java syntaxjava
static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| predicate | Predicate<? super T> | Partition decision |
Return Value
Collector<T,?,Map<Boolean,List<T>>>
Separates values into true and false lists; both keys exist even for an empty partition.
Working Example
Example Snippetjava
import java.util.*;import java.util.stream.*;public class Main { public static void main(String[] args) { var parts = Stream.of(4, 8).collect(Collectors.partitioningBy(n -> n > 0)); System.out.println(parts.get(true)); System.out.println(parts.get(false)); }}