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 bundle has the interface Queue, which extends the Assortment interface. It’s employed to protect the elements which are dealt with based on the FIFO precept. It’s an ordered checklist of things the place new parts are added on the finish and outdated parts are faraway from the start.

Being an interface, the queue wants a concrete class for the declaration, and the preferred lessons in Java are the LinkedList and PriorityQueue. These lessons’ implementations usually are 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 ingredient into the queue and returns true if profitable.
boolean supply(object) Inserts the required ingredient 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 ingredient() 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 pinnacle 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, corresponding to deletion, insertion, and so on., are supported by the Java Queue.
  • LinkedList, ArrayBlockingQueue, and PriorityQueue are the preferred implementations of queue.
  • Any null operation on the blocking queues ends in the NullPointerException being thrown.
  • Unbounded Queues are these Queues which are included within the util bundle.
  • Bounded Queues are these Queues which are included within the util.concurrent bundle.
  • All queues, aside from the Deques, make it simple to get out and in at the back and front of the road, respectively. Deques really permit for ingredient elimination and insertion at each ends.

Implementation of Queue :

Java

import java.util.LinkedList;

import java.util.Queue;

  

public class QueueDemo {

  

    public static void fundamental(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 ingredient - "

                           + x);

  

        System.out.println(q);

  

        

        int head = q.peek();

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

                           + head);

  

        int dimension = q.dimension();

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

                           + dimension);

    }

}

Output

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

PriorityQueue Class

One other class outlined within the assortment framework, PriorityQueue, gives a technique for prioritising objects as they’re processed. Within the Java queue, object insertion and deletion are described as following a FIFO sample. Nonetheless, 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 ingredient 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 incorporates(Object o) Checks whether or not the given ingredient o is current within the PriorityQueue. if sure, returns true.
Iterator< E >iterator() Will get an iterator for the given PriorityQueue.
boolean supply(E e) Insert given ingredient e to the PriorityQueue.
E peek() Used to return the pinnacle of the queue with out deleting the ingredient.
E ballot() If the queue is empty, returns null in any other case it removes and returns the pinnacle 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 lessons corresponding to Assortment, AbstractCollection, AbstractQueue, and Object.
  • The top/entrance of the queue incorporates the least ingredient based on the pure ordering.
  • The implementation of the precedence queue is just not thread-safe. Due to this fact, we must always use the PriorityBlockingQueue if we wish synchronised entry.

Implementation of PriorityQueue :

Java

import java.util.*;

  

class PriorityQueueTest {

  

    

    public static void fundamental(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 ingredient with increased precedence first.
Enqueue and dequeue accomplished in O(1). Enqueue and dequeue accomplished in O(log n) utilizing binary heaps.
Utilized in algorithms corresponding to Breadth First Search. Utilized in algorithms corresponding to Dijkstra’s Algorithm, Prim’s Algorithms, CPU Scheduling.





[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *