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

Output:

{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

Output:

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

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Get great deals at Amazon




Leave a Reply

Notify of
avatar
wpDiscuz