Java PriorityQueue contains(Object) 方法



描述

Java PriorityQueue contains(Object) 方法如果此队列包含指定元素则返回true。对于自定义对象,应实现 equals() 方法,以便 contains 可以正确比较对象。

声明

以下是java.util.PriorityQueue.contains() 方法的声明

public boolean contains(Object o) 

参数

o - 要检查是否包含在队列中的对象。

返回值

如果此队列包含指定的元素,则此方法返回true,否则返回false

异常

检查整数 PriorityQueue 中元素是否存在示例

以下示例演示了 Java PriorityQueue contains() 方法的使用。我们使用的是 Integer。我们将添加一些元素,然后检查某些元素是否存在。

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");    
      }
   }
}           

输出

让我们编译并运行上述程序,这将产生以下结果:

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

检查字符串 PriorityQueue 中元素是否存在示例

以下示例演示了 Java PriorityQueue contains() 方法与字符串一起使用。我们将添加一些元素,然后检查某些元素是否存在。

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");    
      }
   }
}           

输出

让我们编译并运行上述程序,这将产生以下结果:

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

检查对象 PriorityQueue 中元素是否存在示例

以下示例演示了 Java PriorityQueue contains() 方法与 Student 对象一起使用。我们将添加一些元素,然后检查某些元素是否存在。

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;
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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
广告