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:
PriorityBlockingQueue add() Method in Java
Next article icon

PriorityBlockingQueue add() Method in Java

Last Updated : 13 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. 

Syntax:

public boolean add(E e)

Parameter: This method takes a mandatory parameter e which is the element to be inserted in PriorityBlockingQueue. 

Returns: This method returns a boolean response. It returns true if the adding of the element is successful, else it returns false. 

Exception: This method throws following exceptions:

  • ClassCastException: if the element passed as parameter cannot be compared to the element contained by queue to keep priority queue's ordering.
  • NullPointerException: if the element passed as parameter is null.

Below program illustrate  add() method of PriorityBlockingQueue: 

Example 1: 

Java
// Java Program to Demonstrate add(E e) method // of PriorityBlockingQueue.  import java.util.concurrent.PriorityBlockingQueue;  public class GFG {      public static void main(String[] args)     {         // define capacity of PriorityBlockingQueue         int capacity = 15;          // create object of PriorityBlockingQueue         PriorityBlockingQueue<Integer> PrioBlockingQueue             = new PriorityBlockingQueue<Integer>(capacity);          // add  numbers         PrioBlockingQueue.add(526734);         PrioBlockingQueue.add(84879456);         PrioBlockingQueue.add(4586415);          // print queue after add operation         System.out.println("After Adding Some Numbers");         System.out.println("PriorityBlockingQueue:"                            + PrioBlockingQueue);          // add more numbers         PrioBlockingQueue.add(156116);         PrioBlockingQueue.add(61651191);          // print queue after add operation         System.out.println("\nAfter adding Some More Numbers");         System.out.println("PriorityBlockingQueue:"                            + PrioBlockingQueue);     } } 
Output:
After Adding Some Numbers PriorityBlockingQueue:[526734, 84879456, 4586415]  After adding Some More Numbers PriorityBlockingQueue:[156116, 526734, 4586415, 84879456, 61651191]

Example 2: To demonstrate NullPointerException thrown by add() method. 

Java
// Java Program to Demonstrate Exception // thrown by add(E e) method // of PriorityBlockingQueue.  import java.util.concurrent.PriorityBlockingQueue;  public class GFG {      public static void main(String[] args)     {         // define capacity of PriorityBlockingQueue         int capacity = 15;          // create object of PriorityBlockingQueue         PriorityBlockingQueue<Integer> PrioBlockingQueue             = new PriorityBlockingQueue<Integer>(capacity);          // add numbers         PrioBlockingQueue.add(526734);         PrioBlockingQueue.add(84879456);         PrioBlockingQueue.add(4586415);         try {             // try to add null to PrioBlockingQueue             PrioBlockingQueue.add(null);              // print PrioBlockingQueue after add operation             System.out.println("PriorityBlockingQueue:"                                + PrioBlockingQueue);         }         catch (Exception e) {             System.out.println("Exception when adding null: "                                + e);         }     } } 
Output:
Exception when adding null: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#add-E-


Next Article
PriorityBlockingQueue add() Method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • Java-PriorityBlockingQueue
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    PriorityBlockingQueue clear() method in Java
    The clear() method of PriorityBlockingQueue removes all the elements from this queue. Therefore this method can be applied when it is required to clear the PriorityBlockingQueue. Syntax: public void clear() Parameter: This method takes no parameters. Returns: This method returns nothing. Exception:
    2 min read
    PriorityBlockingQueue put() method in Java
    The put(E e) method of PriorityBlockingQueue is used to add an element into this queue. This method inserts the specified element into this priority queue. Since the queue is unbounded, this method will be never be blocked.Syntax: public void put(E e) Parameter: This method accepts a mandatory param
    2 min read
    PriorityBlockingQueue drainTo() method in Java
    The drainTo(Collection col) method of PriorityBlockingQueue removes all available elements from this LinkedBlocking Queue and adds them to the given collection passed as a parameter. drainTo(Collection<? super E> col) The drainTo(Collection<? super E> col) method of PriorityBlockingQueue
    5 min read
    PriorityQueue add() Method in Java
    The Java.util.PriorityQueue.add() method in Java is used to add a specific element into a PriorityQueue. This method internally just calls the Java.util.PriorityQueue.offer() method with the value passed to it. So, it exactly works like offer() method. Syntax: Priority_Queue.add(Object element) Para
    2 min read
    PriorityBlockingQueue take() method in Java
    The take() method of PriorityBlockingQueue returns head of the queue after removing it. If queue is empty, then this method will wait until an element becomes available. Syntax: public E take() throws InterruptedException Returns: This method returns value at the head of this PriorityBlockingQueue.
    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