- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PriorityBlockingQueue Class in Java
The PriorityBlockingQueue Class in Java has a blocking queue that has unbounded functionality and is based on the class PriorityQueue with the same ordering rules. The PriorityBlockingQueue Class is a part of the Java Collection Framework.
A program that demonstrates this is given as follows −
Example
import java.util.concurrent.PriorityBlockingQueue; public class Demo { public static void main(String[] args) { PriorityBlockingQueue<String> pbQueue = new PriorityBlockingQueue<String>(); pbQueue.add("James"); pbQueue.add("May"); pbQueue.add("John"); pbQueue.add("Sara"); pbQueue.add("Anne"); System.out.println("The elements in PriorityBlockingQueue are: " + pbQueue); } }
The output of the above program is as follows −
Output
The elements in PriorityBlockingQueue are: [Anne, James, John, Sara, May]
Now let us understand the above program.
The PriorityBlockingQueue is created and then elements are added to it. Finally, the elements are displayed in order. A code snippet that demonstrates this is given as follows −
PriorityBlockingQueue<String> pbQueue = new PriorityBlockingQueue<String>(); pbQueue.add("James"); pbQueue.add("May"); pbQueue.add("John"); pbQueue.add("Sara"); pbQueue.add("Anne"); System.out.println("The elements in PriorityBlockingQueue are: " + pbQueue);
Advertisements