java.util.PriorityQueue.clear() Method

Advertisements


Description

The clear() method is used to remove all of the elements from this priority queue. The queue will be empty after this call returns.

Declaration

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

public void clear()							 

Parameters

  • NA

Return Value

  • NA

Exception

  • NA

Example

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

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);
      
      // clear the queue
      prq.clear();
      
      System.out.println ( "Priority queue values after clear: " + 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]
Priority queue values after clear: []



Advertisements
Advertisements