contains(Object o)方法檢查PriorityBlockingQueue是否包含對象o。當且僅當此隊列包含至少一個等於作為參數傳遞的對象o的元素e,即equals(o)時,此方法返回true。如果queue不包含Object o,則方法返回false。
用法:
public boolean contains(Object o)
參數:此方法采用強製性參數o,該參數是要在PriorityBlockingQueue中檢查的對象。
返回值:如果此隊列包含作為參數傳遞的對象,則此方法返回true。否則返回false。
異常:此方法不會引發任何異常。
下麵的程序說明PriorityBlockingQueue的contains()方法:
示例1:
// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
// Add element to PriorityBlockingQueue
PrioQueue.add(4641515);
PrioQueue.add(46514561);
PrioQueue.add(56156);
PrioQueue.add(948964165);
// print PrioQueue
System.out.println("PrioQueue: " + PrioQueue);
// check whether PriorityBlockingQueue contains 56156
boolean answer1 = PrioQueue.contains(56156);
// print result
System.out.println("PriorityBlockingQueue contains "
+ "number 56156 : "
+ answer1);
// check whether PriorityBlockingQueue contains 46545
boolean answer2 = PrioQueue.contains(46545);
// print result
System.out.println("PriorityBlockingQueue contains"
+ " number 46545 : "
+ answer2);
}
}
輸出:
PrioQueue: [56156, 46514561, 4641515, 948964165] PriorityBlockingQueue contains number 56156 : true PriorityBlockingQueue contains number 46545 : false
示例2:演示PriorityBlockingQueue的contains()方法,其中包含名稱列表。
// Java Program Demonstrate contains(Object o)
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 10;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
// Add names
names.add("Geeks");
names.add("forGeeks");
names.add("A");
names.add("Computer");
names.add("Portal");
// print queue details
System.out.println("List of Names: " + names);
// check whether PriorityBlockingQueue contains Geeks
boolean answer1 = names.contains("Geeks");
// print result
System.out.println("Does names contains "
+ "Geeks: "
+ answer1);
// check whether PriorityBlockingQueue contains SandeepJain
boolean answer2 = names.contains("SandeepJain");
// print result
System.out.println("Does names contains "
+ "SandeepJain: "
+ answer2);
}
}
輸出:
List of Names: [A, Computer, Geeks, forGeeks, Portal] Does names contains Geeks: true Does names contains SandeepJain: false
相關用法
- Java PriorityBlockingQueue add()用法及代碼示例
- Java PriorityBlockingQueue take()用法及代碼示例
- Java PriorityBlockingQueue put()用法及代碼示例
- Java PriorityBlockingQueue toString()用法及代碼示例
- Java PriorityBlockingQueue comparator()用法及代碼示例
- Java PriorityBlockingQueue size()用法及代碼示例
- Java PriorityBlockingQueue remainingCapacity()用法及代碼示例
- Java PriorityBlockingQueue remove()用法及代碼示例
- Java PriorityBlockingQueue iterator()用法及代碼示例
- Java PriorityBlockingQueue peek()用法及代碼示例
- Java PriorityBlockingQueue spliterator()用法及代碼示例
- Java PriorityBlockingQueue offer()用法及代碼示例
- Java PriorityBlockingQueue toArray()用法及代碼示例
- Java PriorityBlockingQueue poll()用法及代碼示例
- Java PriorityBlockingQueue clear()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 PriorityBlockingQueue contains() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。