Partitioning Operations

In Java 8 Streams, partitioning is a way to split a stream’s elements into two groups based on a predicate (a condition that returns true or false). This is achieved using the Collectors.partitioningBy collector, which groups elements into a Map<Boolean, List<T>>, where the keys are true (elements matching the predicate) and false (elements not matching the predicate).
Syntax
Map<Boolean, List<T>> result = stream.collect(Collectors.partitioningBy(predicate));

predicate: A Predicate<T> that evaluates each element to true or false.
T: The type of elements in the stream.Code language: JavaScript (javascript)

Examples

1. Basic Partitioning

Partition a list of numbers into even and odd numbers:

import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        Map<Boolean, List<Integer>> evenOddPartition = numbers.stream()
                .collect(Collectors.partitioningBy(n -> n % 2 == 0));

        System.out.println("Even numbers: " + evenOddPartition.get(true));
        System.out.println("Odd numbers: " + evenOddPartition.get(false));
    }
}

/*
Even numbers: [2, 4, 6]
Odd numbers: [1, 3, 5]
*/

2. Partitioning with Downstream Collector

You can use a downstream collector to further process the partitioned groups, e.g., to collect results into a Set instead of a List:

Map<Boolean, Set<Integer>> evenOddSet = numbers.stream()
        .collect(Collectors.partitioningBy(
                n -> n % 2 == 0,
                Collectors.toSet()
        ));

System.out.println("Even numbers (Set): " + evenOddSet.get(true));
System.out.println("Odd numbers (Set): " + evenOddSet.get(false));
/*
Even numbers (Set): [2, 4, 6]
Odd numbers (Set): [1, 3, 5]
*/Code language: JavaScript (javascript)

3. Partitioning with Complex Objects

Partition a list of strings based on their length:

List<String> words = Arrays.asList("ant", "dog", "elephant", "rat", "rhinoceros");
Map<Boolean, List<String>> lengthPartition = words.stream()
        .collect(Collectors.partitioningBy(s -> s.length() > 3));

System.out.println("Words with length > 3: " + lengthPartition.get(true));
System.out.println("Words with length <= 3: " + lengthPartition.get(false));

/*
Words with length > 3: [elephant, rhinoceros]
Words with length <= 3: [ant, dog, rat]
*/Code language: JavaScript (javascript)

4. Counting Elements in Each Partition

Use a downstream collector to count elements in each partition:

Map<Boolean, Long> lengthCount = words.stream()
        .collect(Collectors.partitioningBy(
                s -> s.length() > 3,
                Collectors.counting()
        ));

System.out.println("Count of words with length > 3: " + lengthCount.get(true));
System.out.println("Count of words with length <= 3: " + lengthCount.get(false));
/*
Count of words with length > 3: 2
Count of words with length <= 3: 3
*/Code language: JavaScript (javascript)

When to Use

  • When you need to split data into two groups based on a binary condition.
  • When you want to perform additional aggregations (e.g., counting, summing) on the partitioned groups using downstream collectors.

Partitioning operations in Java Streams provide a simple and efficient way to divide elements into two groups based on a predicate. The Collectors.partitioningBy() method makes it easy to implement binary classification and can be combined with other collectors for more complex data analysis. This capability is particularly useful for tasks that require a clear separation of elements into two categories, enhancing the clarity and effectiveness of your data processing logic.

Scroll to Top