Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Priority BlockingQueue
2.1.
Syntax
3.
Hierarchy
4.
The Constructor of Priority BlockingQueue
4.1.
Method 1
4.2.
Example
4.3.
Method 2
4.4.
Example
4.5.
Method 3
4.6.
Example
5.
Methods of PriorityBlockingQueue
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024

Priority Blocking Queue

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

This article will cover the Priority Blocking Queue, but before moving to the actual topic, we need to cover certain things, like a Priority Queue. A Priority Queue is used when the object is supposed to be processed based on its priority. It is also called Queue as it allows the First-In-First-Out algorithm. The elements in the priority queue are ordered according to the natural ordering.

Also read, Duck Number in Java and Hashcode Method in Java

Priority BlockingQueue

Java's Priority Blocking Queue class has a blocking queue with unbounded functionality. It is based on the class Priority Queue with the same ordering rule as the java.util.PriorityQueue class. In this Queue, you cannot insert a null value. All elements in Priority Blocking Queue do not enforce any specific pattern for equal priority elements.

Syntax

public class PriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable

Here, E is the type of elements held in this collection.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Hierarchy

Also see,  Swap Function in Java

The Constructor of Priority BlockingQueue

Method 1

PriorityBlockingQueue(): This will create a PriorityBlockingQueue with the initial default capacity (11). It orders the element according to their natural ordering. Adding elements more than the initial capacity changes the capacity of the PriorityBlockingQueue dynamically.

PriorityBlockingQueue<E> pbq = new PriorityBlockingQueue<E>();

Example

import java.util.concurrent.PriorityBlockingQueue;
public class PriorityBlocking {


public static void main(String[] args) {

    PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue <Integer> ();


        pbq.add(1);
        pbq.add(2);
        pbq.add(3);
        pbq.add(4);
        pbq.add(5);


    System.out.println("PriorityBlockingQueue:" + pbq);
    }
}

Output

PriorityBlockingQueue:[1, 2, 3, 4, 5]

In this example, here we created the Priority Blocking Queue. We have also added them to the Queue and printed them in Queue.

Method 2

PriorityBlockingQueue(Collection<? Extends E>c): It helps us create a PriorityBlockingQueue containing the elements in the specified collection.

PriorityBlockingQueue<E> pbq = new PriorityBlockingQueue(Collection<? Extends E>c)

Example

import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class PriorityBlockingCollection {


public static void main(String[] args) {
Vector<Integer> v = new Vector<Integer>();
v.addElement(10);
v.addElement(20);
v.addElement(30);
v.addElement(40);
v.addElement(50);
  
  PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<Integer>(v);
System.out.println("PriorityBlockingQueue:" + pbq);
}
}

Output

PriorityBlockingQueue:[10, 20, 30, 40, 50]

In this example, we have used the Priority Blocking Queue to add the elements and print them in the end.

Method 3

PriorityBlockingQueue(int initialCapacity): This will create a PriorityBlockingQueue with the specified initial capacity, which orders its elements according to their natural ordering.

Example

import java.util.concurrent.PriorityBlockingQueue;
public class PriorityBlockingCapacity {


public static void main(String[] args) {
int capacity = 15;
  
PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue<String>(capacity);
  
pbq.add("Hello");
pbq.add("Coding");
pbq.add("Ninjas");   
        
System.out.println("PriorityBlockingQueue:" + pbq);
}
}

Output

PriorityBlockingQueue:[Coding, Hello, Ninjas]

We have added a few strings to the priority queue in this example and printed them. Also try it on java online compiler.

 

Methods of PriorityBlockingQueue

Frequently Asked Questions

  1. What is the PriorityBlockingQueue in Java?
    It is an unbounded blocking queue that uses the same governing rules applicable on PriorityQueue. It also supplies blocking and retrieval operations.
     
  2. What is the priority of the priority queue?
    The PriorityQueue is based on the priority heap. The elements of the priority heap are ordered in the natural ordering.
     
  3. How to sort the PriorityBlockingQueue?
    With the help of either Comparator or Comparable implementations to compare elements, these can be sorted.

Conclusion

In this article, we have covered the topic of Priority Blocking Queue. We have briefly introduced the topic. We also explained several topics which might be needed before beginning the topic. We also discussed the Priority Blocking Queue and its syntax. We have shown the hierarchy of Priority Blocking Queue. We also discussed its constructor along with its syntax and several examples. We hope this blog might have helped you enhance your knowledge of PriorityBlockingQueue. If you want to learn more about such topics, please visit Priority Queue in JavaApplication of Priority Queue. We hope that this blog might have helped you in enhancing your knowledge. If you liked this article, please give it a thumbs up, which might help me and other ninjas grow. "Happy Coding!".

Previous article
What is a Circular Queue
Next article
Deque
Live masterclass