Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Collectors partitioningBy() method in Java
Next article icon

Collectors partitioningBy() method in Java

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Collectors partitioningBy() method is a predefined method of java.util.stream.Collectors class which is used to partition a stream of objects(or a set of elements) based on a given predicate. There are two overloaded variants of the method that are present. One takes only a predicate as a parameter whereas the other takes both predicate and a collector instance as parameters.

partitioningBy(Predicate<? super T> predicate)

Syntax:
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
where,
  • Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
    • T: The type of input elements to the reduction operation.
    • A: The mutable accumulation type of the reduction operation.
    • R: The result type of the reduction operation.
  • Map<Boolean, List<T>>: The map containing the output.Keys are boolean values(true or false) and the corresponding values are lists containing elements of type T.
Parameters: This method takes a mandatory parameter predicate which an instance of a Predicate Interface of type T. Return Value: This method returns a Collector implementing the partitioning operation. Below is an example to illustrate partitioningBy() method: Program: Java
// Java code to show the implementation of // Collectors partitioningBy() function  import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream;  class Gfg {      // Driver code     public static void main(String[] args)     {         // creating an Integer stream         Stream<Integer>             s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);          // using Collectors partitioningBy()         // method to split the stream of elements into         // 2 parts, greater than 3 and less than 3.         Map<Boolean, List<Integer> >             map = s.collect(                 Collectors.partitioningBy(num -> num > 3));          // Displaying the result as a map         // true if greater than 3, false otherwise         System.out.println("Elements in stream "                            + "partitioned by "                            + "less than equal to 3: \n"                            + map);     } } 
Output:
  Elements in stream partitioned by less than equal to 3:   {false=[1, 2, 3], true=[4, 5, 6, 7, 8, 9, 10]}  

partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)

Syntax:
public static <T> Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T>predicate, Collector<? super T, A, D> downstream)
where,
  • Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
    • T: The type of input elements to the reduction operation.
    • A: The mutable accumulation type of the reduction operation.
    • R: The result type of the reduction operation.
  • Map<Boolean, List<T>>: The map containing the output.Keys are boolean values(true or false) and the corresponding values are lists containing elements of type T.
Parameters: This method takes two parameters a predicate which an instance of a Predicate Interface of type T, and a collector which is used for implementing "downstream reduction" and producing the output. Return Value: This method returns a Collector implementing the partitioning operation. Below is an example to illustrate type 2 of partitioningBy() method: Java
// Java code to show the implementation of // Collectors partitioningBy() function  import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream;  class ArraytoArrayList {      // Driver code     public static void main(String[] args)     {         // creating an Integer stream         Stream<Integer>             s = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);          // Using Collectors.counting() method         // to count the number of elements in         // the 2 partitions         Map<Boolean, Long>             map = s.collect(                 Collectors.partitioningBy(                     num -> (num > 3), Collectors.counting()));          // Displaying the result as a map         // true if greater than 3, false otherwise         System.out.println("Elements in stream "                            + "partitioned by "                            + "less than equal to 3: \n"                            + map);     } } 
Output:
  Elements in stream partitioned by less than equal to 3:   {false=3, true=7}  

Next Article
Collectors partitioningBy() method in Java

S

SouravAChowdhury_97
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Functions
  • java-stream
  • Java-Stream-Collectors
  • Java 8
  • Java-Collectors
Practice Tags :
  • Java

Similar Reads

    Java Collection retainAll() Method
    In Java, the retainAll() method of Java Collection retains or keeps only those elements present inside the collection which is given as an argument to the function. Syntax for retainAll() method: boolean retainAll(Collection<?> c);Parameters: c is the collection containing elements retained or
    3 min read
    Collectors toList() method in Java with Examples
    The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
    2 min read
    Java Collection removeIf() Method
    The removeIf() method of Java Collection removes all the elements of the calling collection that satisfy the given predicate which is passed as a parameter to the function. If the predicate is true, then it removes the item from the calling collection otherwise it keeps the item in the collection. S
    3 min read
    Collection add() Method in Java with Examples
    The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
    4 min read
    Collections list() method in Java with Examples
    The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences