java.util.PriorityQueue.add() Method


Advertisements


Description

The add(E e) method is used to insert the specified element into a priority queue.

Declaration

Following is the declaration for java.util.PriorityQueue.add() method.

public boolean add(E e)

Parameters

e − The element to be added.

Return Value

  • The method call returns true (as specified by Collection.add(E))

Exception

  • ClassCastException − Throws if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.

  • NullPointerException − Throws if the specified element is null.

Example

The following example shows the usage of java.util.PriorityQueue.add()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class PriorityQueueDemo {
   public static void main(String args[]) {

      // create priority queue
      PriorityQueue < Integer >  prq = new PriorityQueue < Integer > () ; 

      // insert values in the queue
      for ( int i = 0; i  <  10; i++ ) {
         prq.add (new Integer (i)) ; 
      }
      System.out.println("Priority queue values are: " + prq) ; 
   }
}

Let us compile and run the above program, this will produce the following result.

Priority queue values are: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

java_util_priorityqueue.htm


Advertisements
E-Books Store