
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Priority Queue and Queue Implementation in Java
The queue is a linear data structure that inserts elements from the back and removes elements from the starting end of the queue.
A priority queue is an extended version of the normal queue with priorities for each element. In this tutorial, we learn about the queue and priority queue in Java with individual implementation.
Difference between Priority Queue and Queue in Java
Area |
Priority Queue |
Queue |
---|---|---|
Definition |
A priority queue is the queue in which each of its elements has some priorities. The elements from the queue are removed based on their priorities. |
Queue is an interface in Java that uses FIFO principle to remove its elements. |
Types |
Min priority queue and Max priority Queue. |
It has no type. |
Structure |
Each element in priority queue has priority. |
The queue elements have no priority. |
deQueue operation |
Elements are removed on the basis of their highest priority. |
Elements from the queue are removed in FIFO order. |
Element ordering |
It is an ordered queue making searching easy. |
It is a randomly organized queue. |
Complexity |
It is difficult to implement a priority queue. |
It is a simple queue with easy implementation. |
Syntax |
PriorityQueue <data type> queue_name = new PriorityQueue<>(); |
Queue<data type> queue_name = new LinkedList<>(); |
Properties |
Priority Queue inherits the methods of AbstractCollection, AbstractQueue, Object and Collection Class. |
It uses queue interface and util package for implementing queue in Java. |
Operation |
It is not easy to enQueue and deQueue elements. |
It is very easy to insert and remove elements. |
Advantage |
It is easy to deQueue the highest order element. |
Queue does not waste memory and utilize it effectively. |
Disadvantage |
It takes more time in insertion and deletion of elements. |
It has limited space and is not ordered. |
deQueue and enQueue time complexity |
O(log(n)) |
O(1) |
Implementation of Priority Queue and Queue in Java
Example 1
Queue Implementation in Java
Syntax for queue
Queue<data type> queue_name = new LinkedList<>();
Code to Implement Queue in java
import java.util.*; // importing util package with all its features public class Main { public static void main(String[] args) { Queue<Integer> q = new LinkedList<>(); // queue declaration q.add(5); //adding elements to the queue q.add(6); q.add(4); q.add(1); q.add(8); System.out.println("Queue is" + q); System.out.println("Removing queue element: " + q.remove()); System.out.println("Now the Queue is: " + q); } }
Output
Queue is [5, 6, 4, 1, 8] Removing queue element: 5 Now the Queue is: [6, 4, 1, 8]
Example 2
Priority Queue Implementation in Java
Syntax of Priority Queue
PriorityQueue <data type> queue_name = new PriorityQueue<>();
Code to Implement Priority Queue in Java
import java.util.*; public class PriorityQueueExample { public static void main(String[] args) { //declaring priority queue q of string type PriorityQueue <String> p = new PriorityQueue<>(); // inserting elements into the priority queue p.add("Life"); p.add("is"); p.add("Coding"); System.out.println("Priority Queue is " + p); } }
Output
Priority Queue is [Coding, is, Life]
Example 3
poll() method in Priority Queue
import java.util.*; public class PriorityQueueExample { public static void main(String[] args) { PriorityQueue <Integer> p = new PriorityQueue<>(); p.add(5); p.add(7); p.add(1); System.out.println("Priority Queue is: " + p); int i = p.poll(); System.out.println("Head element: " +i); System.out.println("Queue after removing head element: "+p); } }
Output
Priority Queue is: [1, 7, 5] Head Element: [1] Queue after removing head element: [5, 7]
Conclusion
Queue in Java is a linear data structure and is used in the Breadth First Search algorithm. Priority Queue is an extension of normal queue and it is an interface in java with associated priority with each of its elements. One can use various Queue functions to use its features.