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 clear() method in Java
Next article icon

PriorityBlockingQueue clear() method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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: This method does not throw any Exception. Below program illustrate removing all the elements from PriorityBlockingQueue using clear() method. Example 1: Java
// Java Program to Demonstrate clear() 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(78758575);         PrioBlockingQueue.add(63447688);         PrioBlockingQueue.add(56434788);          // print queue after add operation         System.out.println("After Adding  Numbers:");         System.out.println("PriorityBlockingQueue:"                            + PrioBlockingQueue);          // remove all the elements using clear() method         PrioBlockingQueue.clear();          // print queue after clear operation         System.out.println("\nAfter clear operation:");         System.out.println("PriorityBlockingQueue:"                            + PrioBlockingQueue);     } } 
Output:
  After Adding  Numbers:  PriorityBlockingQueue:[56434788, 78758575, 63447688]    After clear operation:  PriorityBlockingQueue:[]  
Example 2: To illustrate clear method on a PriorityBlockingQueue which contains a list of names. Java
// Java Program to Demonstrate clear() 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<String> PrioBlockingQueue             = new PriorityBlockingQueue<String>(capacity);          // add some names         PrioBlockingQueue.add("Tandrima");         PrioBlockingQueue.add("Argha");         PrioBlockingQueue.add("Arka");          // print queue after add operation         System.out.println("List of Names:");         System.out.println("PriorityBlockingQueue: "                            + PrioBlockingQueue);          // remove all the elements using clear() method         PrioBlockingQueue.clear();          // print queue after clear operation         System.out.println("\nAfter clearing List of names:");         System.out.println("PriorityBlockingQueue:"                            + PrioBlockingQueue);     } } 
Output:
  List of Names:  PriorityBlockingQueue: [Argha, Tandrima, Arka]    After clearing List of names:  PriorityBlockingQueue:[]  
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#clear--

Next Article
PriorityBlockingQueue clear() method in Java

A

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

Similar Reads

    PriorityBlockingQueue add() Method in Java
    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 m
    2 min read
    PriorityBlockingQueue comparator() method in Java
    The comparator() method of PriorityBlockingQueue returns the comparator that can be used to order the elements in a PriorityBlockingQueue. The method returns null value if the queue follows the natural ordering pattern of the elements. Syntax: public Comparator<? super E> comparator() Returns:
    2 min read
    PriorityQueue clear() Method in Java
    The Java.util.PriorityQueue.clear() method is used to remove all the elements from a PriorityQueue. Using the clear() method only clears all the element from the queue and does not delete the queue. In other words, we can say that the clear() method is used to only empty an existing PriorityQueue. S
    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
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