Java.util.PriorityQueue.contains()方法用于检查PriorityQueue中是否存在特定元素。因此,本质上,它用于检查队列是否包含任何特定元素。
用法:
Priority_Queue.contains(Object element)
参数:参数element 是PriorityQueue的类型。如果队列中是否存在此元素,则需要对其进行测试。
返回值:如果该元素存在于队列中,则该方法返回True,否则返回False。
以下示例程序旨在说明Java.util.PriorityQueue.contains()方法:
示例1:
// Java code to illustrate contains()
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String args[])
{
// Creating an empty PriorityQueue
PriorityQueue<String> queue = new PriorityQueue<String>();
// Use add() method to add elements into the Queue
queue.add("Welcome");
queue.add("To");
queue.add("Geeks");
queue.add("4");
queue.add("Geeks");
// Displaying the PriorityQueue
System.out.println("PriorityQueue: " + queue);
// Check for "Geeks" in the queue
System.out.println("Does the Queue contains 'Geeks'? "
+queue.contains("Geeks"));
// Check for "4" in the queue
System.out.println("Does the Queue contains '4'? "
+queue.contains("4"));
// Check if the Queue contains "No"
System.out.println("Does the Queue contains 'No'? "
+queue.contains("No"));
}
}
输出:
PriorityQueue: [4, Geeks, To, Welcome, Geeks] Does the Queue contains 'Geeks'? true Does the Queue contains '4'? true Does the Queue contains 'No'? false
示例2:
// Java code to illustrate contains()
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[])
{
// Creating an empty PriorityQueue
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
// Use add() method to add elements into the Queue
queue.add(10);
queue.add(15);
queue.add(30);
queue.add(20);
queue.add(5);
// Displaying the PriorityQueue
System.out.println("PriorityQueue: " + queue);
// Check for '15' in the queue
System.out.println("Does the Queue contains '15'? "
+queue.contains(15));
// Check for '2' in the queue
System.out.println("Does the Queue contains '2'? "
+queue.contains(2));
// Check if the Queue contains '10'
System.out.println("Does the Queue contains '10'? "
+queue.contains(10));
}
}
输出:
PriorityQueue: [5, 10, 30, 20, 15] Does the Queue contains '15'? true Does the Queue contains '2'? false Does the Queue contains '10'? true
相关用法
- Java PriorityQueue add()用法及代码示例
- Java PriorityQueue toArray()用法及代码示例
- Java PriorityQueue remove()用法及代码示例
- Java PriorityQueue size()用法及代码示例
- Java PriorityQueue poll()用法及代码示例
- Java PriorityQueue spliterator()用法及代码示例
- Java PriorityQueue iterator()用法及代码示例
- Java PriorityQueue peek()用法及代码示例
- Java PriorityQueue comparator()用法及代码示例
- Java PriorityQueue offer()用法及代码示例
- Java PriorityQueue clear()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 PriorityQueue contains() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。