Distinction between PriorityQueue and Queue Implementation in Java

[ad_1]



View Dialogue


Enhance Article


Save Article


Like Article



View Dialogue


Enhance Article


Save Article


Like Article








Java Queue Interface

The Java.util package deal has the interface Queue, which extends the Assortment interface. It’s employed to protect the parts which are dealt with in line with the FIFO precept. It’s an ordered checklist of things the place new parts are added on the finish and previous parts are faraway from the start.

Being an interface, the queue wants a concrete class for the declaration, and the preferred courses in Java are the LinkedList and PriorityQueue. These courses’ implementations should not thread-safe. PriorityBlockingQueue is a viable answer if a thread-safe implementation is important.

Declaration :

public interface Queue<E> extends Assortment<E> 

Strategies of Java Queue Interface

Methodology Description
boolean add(object) Inserts the required aspect into the queue and returns true if profitable.
boolean provide(object) Inserts the required aspect into the queue.
Object take away() Used to retrieve and take away the queue’s head.
Object ballot() return null if the queue is empty; else, it obtains and removes queue’s head.
Object aspect() It does retrieve, however doesn’t take away, the queue’s head.
Object peek() returns null if the queue is empty, else it obtains the top of the queue with out eradicating it.

Options of a Queue

  • A queue’s gadgets are added to and eliminated utilizing the FIFO paradigm.
  • All the Assortment interface’s strategies, resembling deletion, insertion, and so forth., are supported by the Java Queue.
  • LinkedList, ArrayBlockingQueue, and PriorityQueue are the preferred implementations of queue.
  • Any null operation on the blocking queues leads to the NullPointerException being thrown.
  • Unbounded Queues are these Queues which are included within the util package deal.
  • Bounded Queues are these Queues which are included within the util.concurrent package deal.
  • All queues, except for the Deques, make it simple to get out and in at the back and front of the road, respectively. Deques really permit for aspect elimination and insertion at each ends.

Implementation of Queue :

Java

import java.util.LinkedList;

import java.util.Queue;

  

public class QueueDemo {

  

    public static void important(String[] args)

    {

        Queue<Integer> q

            = new LinkedList<>();

  

        

        for (int i = 10; i <= 50; i += 10)

            q.add(i);

  

        

        System.out.println("The Parts of the queue are : "

                           + q);

  

        

        int x = q.take away();

        System.out.println("Eliminated aspect - "

                           + x);

  

        System.out.println(q);

  

        

        int head = q.peek();

        System.out.println("Head of the queue - "

                           + head);

  

        int dimension = q.dimension();

        System.out.println("Measurement of the queue - "

                           + dimension);

    }

}

Output

The Parts of the queue are : [10, 20, 30, 40, 50]
Eliminated aspect - 10
[20, 30, 40, 50]
Head of the queue - 20
Measurement of the queue - 4

PriorityQueue Class

One other class outlined within the assortment framework, PriorityQueue, gives a way for prioritising objects as they’re processed. Within the Java queue, object insertion and deletion are described as following a FIFO sample. Nevertheless, a PriorityQueue can be utilized when it’s essential to course of queue parts in accordance with their precedence.

Declaration :

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

Strategies of Java PriorityQueue Class

Methodology Description
boolean add(E e) Provides aspect e to the PriorityQueue.
void clear() Clears the PriorityQueue by deleting all the weather.
Comparatorcomparator() Returns a customized comparator used for the ordering of parts within the Queue.
boolean accommodates(Object o) Checks whether or not the given aspect o is current within the PriorityQueue. if sure, returns true.
Iterator< E >iterator() Will get an iterator for the given PriorityQueue.
boolean provide(E e) Insert given aspect e to the PriorityQueue.
E peek() Used to return the top of the queue with out deleting the aspect.
E ballot() If the queue is empty, returns null in any other case it removes and returns the top of the queue.
int dimension() Returns the variety of parts in PriorityQueue.
Object[] toArray() Used to return an array illustration of the PriorityQueue.
T[] toArray(T[] a) Used to return an array illustration for the Precedence Queue with the identical runtime sort as the required array a.

Traits of a PriorityQueue:

  • PriorityQueue is an unbound queue.
  • PriorityQueue doesn’t allow null values. A precedence queue can’t be created for non-comparable objects.
  • It inherits from courses resembling Assortment, AbstractCollection, AbstractQueue, and Object.
  • The top/entrance of the queue accommodates the least aspect in line with the pure ordering.
  • The implementation of the precedence queue shouldn’t be thread-safe. Due to this fact, we must always use the PriorityBlockingQueue if we would like synchronised entry.

Implementation of PriorityQueue :

Java

import java.util.*;

  

class PriorityQueueTest {

  

    

    public static void important(String args[])

    {

        

        PriorityQueue<String> pq = new PriorityQueue<String>();

  

        

        pq.add("Ram");

        pq.add("Mohan");

        pq.add("Sohan");

  

        

        System.out.println(pq.peek());

  

        

        System.out.println(pq.ballot());

  

        

        System.out.println(pq.peek());

    }

}

Distinction between Queue and PriorityQueue Implementation :

Queue Precedence Queue
Queue is a linear information construction. Precedence Queue is an extension of Queue with precedence issue embedded.
Follows First In First Out (FIFO) algorithm to serve the weather. Serves the aspect with increased precedence first.
Enqueue and dequeue executed in O(1). Enqueue and dequeue executed in O(log n) utilizing binary heaps.
Utilized in algorithms resembling Breadth First Search. Utilized in algorithms resembling Dijkstra’s Algorithm, Prim’s Algorithms, CPU Scheduling.





[ad_2]

Leave a Reply