April 01, 2020

#Collections: Part 3- All about PriorityQueue in Java

What is PriorityQueue in Java?
  • A queue follows First-In-First-Out algorithm, in case of PriorityQueue queue elements are processed according to the priority (ordered as per their natural ordering or based on a custom Comparator supplied at the time of creation).
  • The PriorityQueue is based on the priority heap. 
  • The elements of the PriorityQueue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
  • We can’t create PriorityQueue of Objects that are non-comparable
  • PriorityQueue in Java doesn’t permit null.
  • PriorityQueue are unbound queues.
  • The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements — ties are broken arbitrarily.
  • The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
  • PriorityQueue inherits methods from AbstractQueue, AbstractCollection, Collection and Object class.
PriorityQueue Constructors
  • PriorityQueue(): Creates a PriorityQueue with the default initial capacity (which is 11) that orders its elements according to their natural ordering.
  • PriorityQueue(Collection c): It 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 comparator): Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
  • PriorityQueue(PriorityQueue c): Creates a PriorityQueue containing the elements in the another priority queue.
  • PriorityQueue(SortedSet c): Creates a PriorityQueue containing the elements in the specified sorted set.

PriorityQueue operations
  • boolean add(E element)inserts the specified element into this priority queue.
  • boolean offer(E e) method is used to insert a specific element into the priority queue.
  • public peek() retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
  • public poll()retrieves and removes the head of this queue, or returns null if this queue is empty.
  • public remove() removes a single instance of the specified element from this queue, if it is present. When we remove an element from the priority queue, the least element according to the specified ordering is removed first.
  • Iterator iterator() returns an iterator over the elements in this queue.
  • boolean contains(Object o) method returns true if this queue contains the specified element
  • void clear() is used to remove all of the contents of the priority queue.
  • int size() return the number of elements present in the set.
  • toArray() is used to return an array containing all of the elements in this queue.
  • Comparator comparator() method is used to return the comparator that can be used to order the elements of the queue.
GIT URL: PriorityQueue Example in Java
Priority Queue Example
-K Himaanshu Shuklaa..

No comments:

Post a Comment