Java Stream API Program · OT18
Partition Numbers into Even and Odd Using Streams in Java
Collectors.partitioningBy() splits a stream into exactly two groups using a true-or-false test. Both groups are always present, even when one is empty.
Problem Statement
Given a list of integers, return a map with two entries: true for the even numbers and false for the odd numbers, each keeping the original order.
Both keys must be present even if one group is empty. Negative numbers must be classified correctly.
Input/Output Examples
[10, 15, 22, 33, 40]Output{false=[15, 33], true=[10, 22, 40]}Explanation: each number goes into its group in order.[]Output{false=[], true=[]}Explanation: both groups exist, but they are empty.[-3, -4]Output{false=[-3], true=[-4]}Explanation: -4 % 2 is 0, so it is even.Visual Explanation
n % 2 == 0: even numbers (purple) go to the true list and odd numbers (amber) to the false list.The predicate is evaluated once per element. Unlike groupingBy(), partitioningBy() always creates both keys, so code that reads get(true) never gets null.
Approaches
The collector does the split in one expression. The loop shows what it does underneath.
Approach 1: Collectors.partitioningBy() (Recommended)
Idea: collect the stream with partitioningBy(n -> n % 2 == 0).
- Stream the list.
- Pass the even test to
partitioningBy(). - Read the groups with
get(true)andget(false).
Time: O(n) · Space: O(n).
Approach 2: Manual Loop
Idea: create both lists first, then add each number to the list chosen by the test.
- Put
falseandtruekeys with empty lists into a map. - For each number, add it to
result.get(n % 2 == 0). - Return the map.
Time: O(n) · Space: O(n).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Collectors.partitioningBy() (Recommended)
A second argument, such as Collectors.counting(), would give counts instead of lists.
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class PartitionStream {
// partitioningBy() always returns both keys, false and true, even if a group is empty
static Map<Boolean, List<Integer>> evenOdd(List<Integer> numbers) {
return numbers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0));
}
public static void main(String[] args) {
List<List<Integer>> inputs = List.of(List.of(10, 15, 22, 33, 40), List.of(), List.of(-3, -4));
for (List<Integer> list : inputs) {
System.out.println(list + " -> " + evenOdd(list));
}
}
}
[10, 15, 22, 33, 40] -> {false=[15, 33], true=[10, 22, 40]}
[] -> {false=[], true=[]}
[-3, -4] -> {false=[-3], true=[-4]}
Time complexity: O(n) · Space complexity: O(n).
Approach 2: Manual Loop
Uses a TreeMap so the printed key order (false, then true) matches the collector.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class PartitionLoop {
// The same result built by hand: a sorted map with both keys created up front
static Map<Boolean, List<Integer>> evenOdd(List<Integer> numbers) {
Map<Boolean, List<Integer>> result = new TreeMap<>();
result.put(false, new ArrayList<>());
result.put(true, new ArrayList<>());
for (int n : numbers) {
result.get(n % 2 == 0).add(n);
}
return result;
}
public static void main(String[] args) {
List<List<Integer>> inputs = List.of(List.of(10, 15, 22, 33, 40), List.of(), List.of(-3, -4));
for (List<Integer> list : inputs) {
System.out.println(list + " -> " + evenOdd(list));
}
}
}
[10, 15, 22, 33, 40] -> {false=[15, 33], true=[10, 22, 40]}
[] -> {false=[], true=[]}
[-3, -4] -> {false=[-3], true=[-4]}
Time complexity: O(n) · Space complexity: O(n).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Collectors.partitioningBy() | O(n) | O(n) |
| Manual Loop | O(n) | O(n) |
Here n is the size of the list. Both approaches test each element once and store every element in one of the two lists.
Interview Tips and Recommended Approach
- Know the difference from
groupingBy().groupingBy()only creates keys that occur;partitioningBy()always has both. - Test for even, not odd.
-3 % 2is-1, son % 2 == 1misclassifies negative odd numbers. - Add a downstream collector for counts or sums, for example
partitioningBy(test, counting()). - Use it for any yes/no split: passed and failed scores, active and inactive users, and so on.
Recommended Approach: Collectors.partitioningBy()
Why: it expresses a two-way split in one readable call and guarantees that both groups exist.