Monday, 24 October 2016

What is PriorityQueue In Java ?

Previously we Have discussed about


Here we learn about PriorityQueue Class in Java, but before that we must know What is Queue Interface Interface in Java? and What is Collection Interface In Java?
Class PriorityQueue<E>

Type Parameters:
E - the type of elements held in this collection

All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>

Declaration of PriorityQueue:
public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at Queue creation time, depending on which constructor is used.A priority queue does not permit null elements.

A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always large as the queue size. As elements are added to a priority queue, its capacity grows automatically. 

The head of this queue is the least element with respect to the specified ordering. The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

PriorityQueue implementation is not synchronized. Multiple threads should not access a PriorityQueue instance concurrently if any of the threads modifies the queue. We can use thread-safe class i.e PriorityBlockingQueue class.
This class is a member of the Java Collections Framework.

Constructor Summary of PriorityQueue:

Constructor
Description
PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
PriorityQueue(Collection<? extends E> c)

Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue(PriorityQueue<? extends E> c)
Creates a PriorityQueue containing the elements in the specified priority queue.
PriorityQueue(SortedSet<? extends E> c)
Creates a PriorityQueue containing the elements in the specified sorted set.
      
Method Summary of PriorityQueue:

Modifier and Method Name
Description
boolean add(E e)
Inserts the specified element into this priority queue.
void clear()
Removes all of the elements from this priority queue.
Comparator<? super E>                comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
boolean contains(Object o)
Returns true if this queue contains the specified element.
Iterator<E> iterator()
Returns an iterator over the elements in this queue.
boolean offer(E e)
Inserts the specified element into this priority queue.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
boolean remove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
int size()
Returns the number of elements in this collection.
Object[] toArray()
Returns an array containing all of the elements in this queue.
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

Example of PriorityQueue:

import java.util.PriorityQueue;
import java.util.Queue;

public class PriorityQueueClassExample {

public static void main(String[] args) {
Queue<Integer> que = new PriorityQueue<Integer>();
for(int i=1;i<=5;i++){
que.add(i);
}

System.out.println("Queue Elements : "+que);

System.out.println("\npeek(), This method gives head element : "+que.peek());
System.out.println("poll(), This method remove head element : "+que.poll());
System.out.println("remove(e), This method remove specified element : "+que.remove(2));

System.out.println("\nQueue After Operations : "+que);
}

}
Program Output:

Queue Elements : [1, 2, 3, 4, 5]

peek(), This method gives head element : 1
poll(), This method remove head element : 1
remove(e), This method remove specified element : true

Queue After Operations : [3, 4, 5]



Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment