Collectors partitioningBy() method in Java

In this post, we will discuss partitioningBy() method included in Collectors class in Java 8.


The partitioningBy(predicate) method returns a Collector which partitions the input elements according to a given Predicate, and organizes them into a Map<Boolean, List<T>>.


Example #1: Split a list into two sublists


Download   Run Code


{false=[1, 2, 3], true=[4, 5]}

The first sublist is [1, 2, 3]
The second sublist is [4, 5]


Example #2: Partition students into passed and failed


Download   Run Code


Passed Students : [Tom, Lisa, John, Joe]
Failed Students : [Jason]

Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of