Difference between PriorityQueue and Queue Implementation in Java
Java Queue Interface
The Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed from the beginning.
Being an interface, the queue needs a concrete class for the declaration, and the most popular classes in Java are the LinkedList and PriorityQueue. These classes’ implementations are not thread-safe. PriorityBlockingQueue is a viable solution if a thread-safe implementation is necessary.
public interface Queue<E> extends Collection<E>
Methods of Java Queue Interface
|boolean add(object)||Inserts the specified element into the queue and returns true if successful.|
|boolean offer(object)||Inserts the specified element into the queue.|
|Object remove()||Used to retrieve and remove the queue’s head.|
|Object poll()||return null if the queue is empty; else, it obtains and removes queue’s head.|
|Object element()||It does retrieve, but does not remove, the queue’s head.|
|Object peek()||returns null if the queue is empty, else it obtains the head of the queue without removing it.|
Features of a Queue
- A queue’s items are added to and removed using the FIFO paradigm.
- All of the Collection interface’s methods, such as deletion, insertion, etc., are supported by the Java Queue.
- LinkedList, ArrayBlockingQueue, and PriorityQueue are the most popular implementations of queue.
- Any null operation on the blocking queues results in the NullPointerException being thrown.
- Unbounded Queues are those Queues that are included in the util package.
- Bounded Queues are those Queues that are included in the util.concurrent package.
- All queues, with the exception of the Deques, make it easy to get in and out at the front and back of the line, respectively. Deques actually allow for element removal and insertion at both ends.
Implementation of Queue :
The Elements of the queue are : [10, 20, 30, 40, 50] Removed element - 10 [20, 30, 40, 50] Head of the queue - 20 Size of the queue - 4
Another class defined in the collection framework, PriorityQueue, provides a method for prioritising objects as they are processed. In the Java queue, object insertion and deletion are described as following a FIFO pattern. However, a PriorityQueue can be used when it is necessary to process queue elements in accordance with their priority.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Methods of Java PriorityQueue Class
|boolean add(E e)||Adds element e to the PriorityQueue.|
|void clear()||Clears the PriorityQueue by deleting all the elements.|
|Comparatorcomparator()||Returns a custom comparator used for the ordering of elements in the Queue.|
|boolean contains(Object o)||Checks whether the given element o is present in the PriorityQueue. if yes, returns true.|
|Iterator< E >iterator()||Gets an iterator for the given PriorityQueue.|
|boolean offer(E e)||Insert given element e to the PriorityQueue.|
|E peek()||Used to return the head of the queue without deleting the element.|
|E poll()||If the queue is empty, returns null otherwise it removes and returns the head of the queue.|
|int size()||Returns the number of elements in PriorityQueue.|
|Object toArray()||Used to return an array representation of the PriorityQueue.|
|T toArray(T a)||Used to return an array representation for the Priority Queue with the same runtime type as the specified array a.|
Characteristics of a PriorityQueue:
- PriorityQueue is an unbound queue.
- PriorityQueue does not permit null values. A priority queue cannot be created for non-comparable objects.
- It inherits from classes such as Collection, AbstractCollection, AbstractQueue, and Object.
- The head/front of the queue contains the least element according to the natural ordering.
- The implementation of the priority queue is not thread-safe. Therefore, we should use the PriorityBlockingQueue if we want synchronised access.
Implementation of PriorityQueue :
Mohan Mohan Ram
Difference between Queue and PriorityQueue Implementation :
|Queue is a linear data structure.||Priority Queue is an extension of Queue with priority factor embedded.|
|Follows First In First Out (FIFO) algorithm to serve the elements.||Serves the element with higher priority first.|
|Enqueue and dequeue done in O(1).||Enqueue and dequeue done in O(log n) using binary heaps.|
|Used in algorithms such as Breadth First Search.||Used in algorithms such as Dijkstra’s Algorithm, Prim’s Algorithms, CPU Scheduling.|
Please Login to comment...