Java PriorityQueue contains(Object) Method



Description

The Java PriorityQueue contains(Object) returns true if this queue contains the specified element. In case of custom objects, equals() methd should be implemented so that contains can compare objects correctly.

Declaration

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

public boolean contains(Object o) 

Parameters

o − The object to be checked as contained in queue.

Return Value

This method returns true if this queue contains the specified element, else false.

Exception

NA

Checking an Element's presence in a PriorityQueue of Ints Example

The following example shows the usage of Java PriorityQueue contains() method. We're using Integers. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

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

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

      // use add() method to add elements in the queue
      queue.add(20);
      queue.add(30);
      queue.add(10);
      queue.add(18);        

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // queue contains element 10
      if (queue.contains(10)) {
         System.out.println("element 10 is present in the queue");
      } else {
         System.out.println("element 10 is not present in the queue");
      }

      // queue does not contain element 25
      if (queue.contains(25)) {
         System.out.println("element 25 is present in the queue");
      } else {
         System.out.println("element 25 is not present in the queue");    
      }
   }
}           

Output

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

PriorityQueue = [10, 18, 20, 30]
element 10 is present in the queue
element 25 is not present in the queue

Checking an Element's presence in a PriorityQueue of Strings Example

The following example shows the usage of Java PriorityQueue contains() method with Strings. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

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

      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("Welcome");
      queue.add("to");
      queue.add("tutorialspoint");
      queue.add(".com");        

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // queue contains element tutorialspoint
      if (queue.contains("tutorialspoint")) {
         System.out.println("element tutorialspoint is present in the queue");
      } else {
         System.out.println("element tutorialspoint is not present in the queue");
      }

      // queue does not contain element html
      if (queue.contains("html")) {
         System.out.println("element html is present in the queue");
      } else {
         System.out.println("element html is not present in the queue");    
      }
   }
}           

Output

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

PriorityQueue = [.com, Welcome, tutorialspoint, to]
element tutorialspoint is present in the queue
element html is not present in the queue

Checking an Element's presence in a PriorityQueue of Objects Example

The following example shows the usage of Java PriorityQueue contains() method with Student objects. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;

import java.util.PriorityQueue;

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

      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));      

      // let us print all the elements available in queue
      System.out.println("PriorityQueue = " + queue);

      // queue contains element Robert
      if (queue.contains(new Student(2, "Robert"))) {
         System.out.println("Student Robert is present in the queue");
      } else {
         System.out.println("Student Robert is not present in the queue");
      }

      // queue does not contain element Jane
      if (queue.contains(new Student(4, "Jane"))) {
         System.out.println("Student Jane is present in the queue");
      } else {
         System.out.println("Student Jane is not present in the queue");    
      }
   }
} 
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

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

PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student Robert is present in the queue
Student Jane is not present in the queue
java_util_priorityqueue.htm
Advertisements