java.util.PriorityQueue.contains() Method

Advertisements


Description

The contains(Object o) method is used to return true if this queue contains the specified element.

Declaration

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

public boolean contains(Object o)

Parameters

  • o--The object to be checked for containment in this queue.

Return Value

  • The method call returns 'true' if this queue contains the specified element.

Exception

  • NA

Example

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

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);
      
      // check if queue contains 5
      boolean b = prq.contains(5);
      
      System.out.println ( "Priority queue contains 5: " + b);
   }
}

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 contains 5: true



Advertisements
Advertisements