In this post, we will show we how to use the Collectors.partitioningBy() method in Java to partition a stream of data according to a given predicate.

1. What is the Collectors.partitioningBy() Method?

The Collectors.partitioningBy() method is a static method that returns a Collector that partitions the input elements according to a Predicate, and organizes them into a Map<Boolean, List<T>>. The syntax is as follows:

 
Here, T is the type of the input elements. The predicate parameter is used to test each element of the stream and assign it to one of the two groups: true or false. The map returned by the collector has two keys: true and false, and the corresponding values are lists that contain the elements that belong to each group.

 
This method is useful to partition a stream of data into two groups based on a certain condition. For example, we may have a stream of numbers and we want to split them into even and odd numbers. Or we may have a stream of products and we want to split them into cheap and expensive products. The Collectors.partitioningBy() method can help us with this task.

2. How to use the Collectors.partitioningBy() Method?

Let’s see some examples of how to use the Collectors.partitioningBy() method in Java with different types of streams and predicates.

Example 1: Partitioning a Stream of integers by odd and even numbers

Suppose we have a stream of integers and we want to partition them into even and odd numbers. We can use the Collectors.partitioningBy() method with a predicate that checks if an element is divisible by 2:

Download  Run Code

Example 2: Partitioning a Stream of strings by length

Suppose we have a stream of strings and we want to partition them into short and long words based on their length. We can use the Collectors.partitioningBy() method with a predicate that checks if an element has more than four characters:

Download  Run Code

Example 3: Partitioning a Stream of objects by property

Suppose we have a stream of objects that represent students and we want to partition them into passed and failed students based on their marks. We can use the Collectors.partitioningBy() method with a predicate that checks if a student has a marks more than or equal to a threshold:

Download  Run Code

3. Advantages and Disadvantages of Collectors.partitioningBy() method

Partitioning streams is a common and useful task in Java, as it allows us to split data into groups based on certain criteria. Some of the advantages of the Collectors.partitioningBy() method are:

  • It takes only one method call to partition a stream of data according to a given condition.
  • It can work with any type of stream and predicate, as long as they are compatible.
  • It does not modify the original stream or its elements, but returns a new map as the result.

The Collectors.partitioningBy() method also has some disadvantages that we should be aware of before using it:

  • It can only partition a stream into only two groups. If you need more than two groups, we may need to use other methods, such as groupingBy() or mapping().
  • It creates a new map and two new lists for each partitioning operation, which may consume more memory or time than necessary.

That’s all about the Collectors.partitioningBy() method in Java.