< Prev
Next >



PriorityQueue Class





PriorityQueue is a collection class that extends AbstractQueue class and implements the Queue interface. While Queue is a first-in, first-out(FIFO) queue of elements, the functionality of PriorityQueue has been enhanced. Priority Queue creates a priority-in, priority-out Queue i.e. its elements are sorted in an ascending order provided by the default Comparator.




Important features of PriorityQueue







Constructors :


1) PriorityQueue()
This constructor creates an empty PriorityQueue of a particular type.
Example -
PriorityQueue<Integer> arrL= new PriorityQueue<Integer>();

This constructor example creates an empty PriorityQueue to hold Integer objects.




2) PriorityQueue(Collection <? extends E>c)
This constructor creates a PriorityQueue initialized with the elements of Collection c. With a condition, that, Collection c holds similar type of objects as the type declared by the PriorityQueue.
PriorityQueue<Integer> arrL1 = new PriorityQueue<Integer>():
PriorityQueue<Integer> arrL2 = new PriorityQueue<Integer>(arrL1);

This constructor example creates a PriorityQueue arrL2 initialized with the elements of a PriorityQueue arrL1. Both PriorityQueue hold Integer objects.




3) PriorityQueue(Comparator<? super E> comp)
This constructor will create an empty PriorityQueue which is sorted according to the specified Comparator(comp) in constructor.
PriorityQueue<String> ts2 = new PriorityQueue<String>(new Comp());






Some important methods of PriorityQueue class


Methods Description
boolean add(E e) Inserts the element e at the end of an PriorityQueue.
void addFirst(E e) Inserts the element e at the beginning of the PriorityQueue.
void addLast(E e) Inserts the element e at the end of PriorityQueue.
E removeFirst() Removes the first element of the PriorityQueue.
E removeLast(E e) Removes the last element of the PriorityQueue.
boolean offer(E e) Inserts the element e in the PriorityQueue.
E peek() Returns the head(first) element of the PriorityQueue, without removing it.
E poll() Returns and removes the head(first) element of the PriorityQueue.
int size() Returns the total number of elements in an ArrayList.
void clear() Removes all of the elements from the ArrayList
E get(int index) Returns the element at the specified index in an ArrayList.
E remove(int index) Removes an element at a specified index in an ArrayList.
boolean remove(Object o) Removes the first occurrence of an Object from the ArrayList, if it is present.
Object[] toArray() Returns an Object array containining all the elements of an ArrayList.
Iterator iterator() Returns an Iterator to iterate over elements of an ArrayList.
ListIterator listIterator() Returns a ListIterator to iterate over elements of an ArrayList.





Let's understand this by an example -:


In this program we have created an PriorityQueue to hold Integer objects and we will add/remove some elements from it using different methods in it and we will access its elements in an ascending order by using poll() method.

import java.util.*;

class PriorityQueue1
{
public static void main(String... ar)
{

PriorityQueue<Integer> priorityQ= new PriorityQueue<Integer>();

priorityQ.offer(4);
priorityQ.offer(1);
priorityQ.offer(5); // Offer method adds an object in the PriorityQueue
priorityQ.add(2);   // add() also adds an object in the PriorityQueue
priorityQ.add(3);



priorityQ.remove(3); // to remove 3 from the PriorityQueue

System.out.println("PriorityQueue contents may appear out-of-order : " + priorityQ);

System.out.print("PriorityQueue in an ascending ordering using poll() method : ");

//poll() returns & removes the first(smallest) element of Queue or returns null, if Queue is empty
for( Integer i= priorityQ.poll(); i!=null; i=priorityQ.poll()) 
{
System.out.print( i + " ");
}

System.out.println("\nSize of PriorityQueue after using poll() = "+ priorityQ.size());
}

}


Output is - :


PriorityQueue contents may appear out-of-order : [1, 2, 5, 4]
PriorityQueue in an ascending ordering using poll() method : 1 2 4 5
Size of PriorityQueue after using poll() = 0


Program Analysis







Implementing Comparator interface in PriorityQueue.


Here in the next example, we are implementing Comparator interface to give a natural ascending order to the elements in PriorityQueue and hence we won't need its poll() method to get elements in an ascending order.

import java.util.*;

class PQSort implements Comparator
{

//Overriding Comparator's compare() method to give a descending ordering
public int compare(Integer one, Integer two) 
{
return(one-two);
}


public static void main(String... ar)
{
//Passing an object of implementer of Comparator Interface
PriorityQueue pq= new PriorityQueue(new PQSort()); 

pq.offer(4);
pq.offer(1);
pq.offer(5); // Offer method adds an object in the PriorityQueue
pq.add(2);   // add() also adds an object in the PriorityQueue
pq.add(3);



pq.remove(3); //removes 3 from the PriorityQueue

System.out.println("Content of PriorityQueue with natural ascending ordering" + pq)

System.out.println("Size of PriorityQueue = "+ pq.size());
}


Output is :


Content of PriorityQueue with natural ascending ordering[1, 2, 5, 4]
Size of PriorityQueue = 4





Implementing Comparator interface in PriorityQueue for descending order.


Here in the next example, we are implementing Comparator interface to give a descending order to the elements in PriorityQueue and hence we don't need any poll() method to have any ordering of objects.

import java.util.*;

class PriorityQSort implements Comparator
{

public int compare(Integer one, Integer two) //overriding Comparator's compare() method to give a descending ordering
{
return(two-one);
}


public static void main(String... ar)
{
PriorityQueue pq= new PriorityQueue(new PriorityQSort()); //passing an object of implementer of Comparator Interface

pq.offer(4);
pq.offer(1);
pq.offer(5); // Offer method adds an object in the PriorityQueue
pq.add(2);   // add() also adds an object in the PriorityQueue
pq.add(3);



pq.remove(3); // to remove 3 from the PriorityQueue

System.out.println("Content of PriorityQueue with descending ordering" + pq);
System.out.println("Size of PriorityQueue = "+ pq.size());


}

}


Output is :


Content of PriorityQueue with descending ordering[5, 2, 4, 1]
Size of PriorityQueue = 

A descending order is given to the elements in PriorityQueue by implementing Comparator interface.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Comparator for ordering
ArrayDeque Class >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON