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]

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of