Java Utility Library

Java.util.PriorityQueue Class



Java PriorityQueue Class

Java.util package provides a PriorityQueue class where an unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects.

Class declaration

The declaration of java.util.PriorityQueue class is:

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

Here, E is the type of element maintained by the container.

Class Constructors

S.NConstructors & Description
1. PriorityQueue()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
2. PriorityQueue(Collection<? extends E> c)
Creates a PriorityQueue containing the elements in the specified collection.
3. PriorityQueue(Comparator<? super E> comparator)
Creates a PriorityQueue with the default initial capacity and whose elements are ordered according to the specified comparator.
4. PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
5. PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
6. PriorityQueue(PriorityQueue<? extends E> c)
Creates a PriorityQueue containing the elements in the specified priority queue.
7. PriorityQueue(SortedSet<? extends E> c)
Creates a PriorityQueue containing the elements in the specified sorted set.

java.util.PriorityQueue Methods

The java.util.PriorityQueue class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean add(E element)
Inserts the specified element into this priority queue.
2. void clear()
Removes all of the elements from this priority queue.
3. 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.
4. boolean contains(Object obj)
Returns true if the queue contains the specified element.
5. Iterator<E> iterator()
Returns an iterator over the elements in this queue.
6. boolean offer(E element)
Inserts the specified element into this priority queue.
7. E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
8. E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
9. boolean remove(Object obj)
Removes a single instance of the specified element from this queue, if it is present.
10. int size()
Returns the number of elements in this queue.
11. Spliterator<E> spliterator()
Creates a late-binding and fail-fast spliterator over the elements in this queue.
12. Object[] toArray()
Returns an array containing all of the elements in this queue.
13. <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.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractQueue<E>