Open In App

Java.util.PriorityQueue class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

It is a priority queue based on priority heap.

  • Elements in this class are in natural order or depends on the Constructor we used at this the time of construction.
  • It doesn’t permit null pointers.
  • It doesn’t allow inserting a non-comparable object, if it relies on natural ordering.

Constructors:

  • PriorityQueue(): Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
  • PriorityQueue(Collection 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 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 specified priority queue.
  • PriorityQueue(SortedSet c): Creates a PriorityQueue containing the elements in the specified sorted set.

Declaration :

public class PriorityQueue
   extends AbstractQueue
   implements Serializable

Methods:

  1. add(element) : java.util.PriorityQueue.add() insert the elements to the Priority Queue.
    Syntax :

    public boolean add(E e)
    Parameters  :
    element : the element we need to add.
    Return  :
    call return true.
    Exception : 
    -&gt ClassCastException 
    -&gt NullPointerException
    
  2. comparator() : java.util.PriorityQueue.comparator() orders the elements in the queue.
    Syntax :

    public Comparator comparator()
    Parameters  :
    -------
    Return  :
    orders the queue or return null, if it is naturally ordered 
    Exception : 
    ----------
    
  3. contains(Object obj) : java.util.PriorityQueue.contains(obj) returns true if the priority queue contains the element “obj”.
    Syntax :

    public boolean contains(Object obj)
    Parameters  :
    obj : object to be checked
    Return  :
    true - if the object is present else, return false
    Exception : 
    
    
  4. iterator() : java.util.PriorityQueue.iterator() iterates over the queue element.
    Syntax :

    public Iterator iterator()
    Parameters  :
    -------
    Return  :
    calls iterator over the elements in the queue.
    Exception : 
    --------
    
  5. offer(element) : java.util.PriorityQueue.offer() is required to insert a specific element to the given priority queue.
    Syntax :

    public boolean offer(E element)
    Parameters  :
    element : specific element to  be entered.
    Return  :
    call return true.
    Exception : 
    -&gt ClassCastException 
    -&gt NullPointerException
    
  6. peek() : java.util.PriorityQueue.peek() identifies the head element of the queue.
    Syntax :

    public E peek()    
    Parameters  :
    -------
    Return  :
    calls if head exists, else null
    Exception : 
    ------
    
  7. poll() : java.util.PriorityQueue.poll() identifies the head and then removes it.
    Syntax :

    public E poll()    
    Parameters  :
    ---
    Return  :
    calls if head exists, else null
    Exception : 
    ------
    
  8. remove(Object obj) : java.util.PriorityQueue.remove() removes a specific object from the queue.
    Syntax :

    public boolean remove(Object obj)
    Parameters  :
    obj : object to be removed
    Return  :
    true - if obj is removed
    Exception : 
    ------
    
  9. size() : java.util.PriorityQueue.size() returns the size of elements in the Priority Queue.
    Syntax :

    public int size()
    Parameters  :
    ----
    Return  :
    no. of elements
    Exception : 
    ---------
    
  10. toArray() : java.util.PriorityQueue.toArray() returns an array containing the elements of PriorityQueue.
    Syntax :

    public Object[] toArray()
    Parameters  :
    ------
    Return  :
    returns an array containing all the elements of PriorityQueue.
    Exception : 
    --------
    
  11. toArray(T[] array) : java.util.PriorityQueue.toArray(T[] a) returns the array having the elements of the Priority Queue.
    Syntax :

    public  T[] toArray(T[] array)
    Parameters  :
    array  : the array to which are to be sorted. 
    Return  :
    call an array containing all the elements of the array. 
    Exception : 
    -> ArrayStoreException
    -> NullPointerException
    
  12. clear() : java.util.PriorityQueue.clear() clears all the elements of the PriorityQueue.
    Syntax :

    public void clear()        
    Parameters  :
    ---
    Return  :
    ------
    Exception : 
    ------
    



  13. // Java Program illustrating the methods
    // of java.utl.priorityQueue class
      
    // add(), comparator(), conatins(), iterator(), offer()
    // peek(), poll(), toArray(), size(), toArray(t[] g1),
    // remove(), clear()
      
    import java.util.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            // Creating a Priority Queue :
            PriorityQueue <Integer> geek = new PriorityQueue <Integer> ();
      
            for(int i=2; i<=20; i=i+2)
            {
                // Use of add() :
                geek.add(new Integer (i));
            }
      
            System.out.println("geek PriorityQueue : " + geek);
      
            // Use of comparator() 
            // No ordering is required here as it is naturally ordered.
            Comparator geek_comp = geek.comparator();
            System.out.println("geek PriorityQueue : " + geek_comp);
      
            // Use of contains() 
            boolean check = geek.contains(6);
            System.out.println("Use of contains() : " + check);
      
            // Use of iterator() 
            Iterator g_iterator = geek.iterator();
      
            System.out.print("Iterator values : ");
            while(g_iterator.hasNext())
            {
                System.out.print(g_iterator.next() + " ");
            }
            System.out.println("");
      
            // Use of offer() 
            geek.offer(3050);
            System.out.println("geek PriorityQueue : " + geek);
      
            // Use of peek() 
            System.out.println("Head of PriorityQueue via peek : " + geek.peek());
      
            //Use of poll() 
            int h = geek.poll();
            System.out.println("\nHead of PriorityQueue via poll : " + h);
            System.out.println("geek PriorityQueue bcz of poll() : " + geek);
      
            // Use of remove()
            boolean r = geek.remove(8);
            System.out.println("\nCan remove : " + r);
            System.out.println("geek PriorityQueue bcz of remove() : " + geek);
      
            // use of size() 
            System.out.println("\nSize of PriorityQueue : " + geek.size());
      
            // Use of toArray() 
            Object[] g = geek.toArray();
            System.out.print ( "Array from PriorityQueue : ");
      
            for ( int i = 0; i<g.length; i++ )
            {
                System.out.print (g[i].toString() + " ") ;
            }
      
            System.out.println("\n");
      
            // Use of toArray(t[] g1) :
            Integer[] g2 = new Integer[5];
            Integer[] g1 = geek.toArray(g2);
            System.out.print ( "Array from PriorityQueue of size 5 : ");
      
            for ( int i = 0; i<g1.length; i++ )
            {
                System.out.print (g1[i].toString() + " ") ;
            }
      
            System.out.println("\n");
      
            // Use of clear() 
            geek.clear();
            System.out.println("PriorityQueue after clear() : " + geek);
      
        }
    }

    
    

    Output :

    geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
    geek PriorityQueue : null
    Use of contains() : true
    Iterator values : 2 4 6 8 10 12 14 16 18 20 
    geek PriorityQueue : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 3050]
    Head of PriorityQueue via peek : 2
    
    Head of PriorityQueue via poll : 2
    geek PriorityQueue bcz of poll() : [4, 8, 6, 16, 10, 12, 14, 3050, 18, 20]
    
    Can remove : true
    geek PriorityQueue bcz of remove() : [4, 10, 6, 16, 20, 12, 14, 3050, 18]
    
    Size of PriorityQueue : 9
    Array from PriorityQueue : 4 10 6 16 20 12 14 3050 18 
    
    Array from PriorityQueue of size 5 : 4 10 6 16 20 12 14 3050 18 
    
    PriorityQueue after clear() : []


Last Updated : 06 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads