Java 8 Collectors.partitioningBy() Method Example


Java 8 Stream API’s Collectors.partitioningBy() method, which offers a powerful way to partition elements of a stream into two groups based on a predicate. In this blog, we’ll explore Collectors.partitioningBy() method signature, providing examples, and discussing its applications and benefits.

Method Signature: The method signature for Collectors.partitioningBy() is as follows:

public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)

Explanation:

  • <T>: Type parameter representing the type of elements in the stream.
  • Collector<T, ?, Map<Boolean, List<T>>>: The returned collector that partitions the input elements into a Map<Boolean, List<T>>, where the key represents the result of the predicate (true or false), and the value is a list of elements that satisfy that predicate.
  • Predicate<? super T> predicate: The predicate used to partition the elements.

Examples: Let’s dive into some examples to better understand how Collectors.partitioningBy() works:

Example 1: Partitioning Numbers into Even and Odd

PartitionBy1.java
package org.websparrow;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PartitionBy1 {

    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

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

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

Output:

console.log
Even numbers: [2, 4, 6, 8, 10]
Odd numbers: [1, 3, 5, 7, 9]

Related Posts:

  1. Java Stream peek() Method Example
  2. Java 8 Stream generate() Method
  3. Java 8 Collectors.groupingBy() Method Example

Example 2: Partitioning Strings by Length

PartitionBy2.java
package org.websparrow;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PartitionBy2 {

    public static void main(String[] args) {

        List<String> words = Arrays.asList("apple", "banana", "grape", "orange", "kiwi");

        Map<Boolean, List<String>> partitionedWords = words.stream()
                .collect(Collectors.partitioningBy(s -> s.length() > 5));

        System.out.println("Words with length greater than 5: " + partitionedWords.get(true));
        System.out.println("Words with length less than or equal to 5: " + partitionedWords.get(false));
    }
}

Output:

console.log
Words with length greater than 5: [banana, orange]
Words with length less than or equal to 5: [apple, grape, kiwi]

Benefits

Collectors.partitioningBy() is incredibly versatile and finds applications in various scenarios, including:

  • Data categorization: Grouping data based on certain characteristics, such as dividing items into two categories (e.g., true/false, even/odd).
  • Filtering: Separating elements that meet specific criteria from those that do not.
  • Performance Optimization: Streamlining data processing pipelines by partitioning data upfront.

References

  1. Collectors.partitioningBy() – JavaDoc

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.