如果ConcurrentLinkedQueue包含作為參數傳遞的對象o,則ConcurrentLinkedQueue的contains()方法返回true。當且僅當此ConcurrentLinkedQueue包含至少一個等於作為參數傳遞的對象o的元素e,即o.equals(e)時,此方法才返回true。
用法:
public boolean contains(Object o)
參數:此方法采用代表對象的單個參數O來檢查ConcurrentLinkedQueue是否包含指定的對象。
返回值:如果此ConcurrentLinkedQueue包含對象,則此方法返回true。
以下示例程序旨在說明ConcurrentLinkedQueue的contains()方法:
示例1:
// Java Program Demonstrate contains()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
queue.add("Sanjeet");
queue.add("Rabi");
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// check whether queue contains String "Aman"
boolean response1 = queue.contains("Aman");
// print after applying contains method
System.out.println("queue contains String \"Aman\" : "
+ response1);
// check whether queue contains "Ram"
boolean response2 = queue.contains("Ram");
// print after applying contains method
System.out.println("queue contains String \"Ram\" : "
+ response2);
}
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi] queue contains String "Aman" : true queue contains String "Ram" : false
示例2:
// Java Program Demonstrate contains()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
// Add Numbers to queue
queue.add(4353);
queue.add(7824);
queue.add(78249);
queue.add(8724);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// check whether queue contains 78249
boolean response1 = queue.contains(78249);
// print after applying contains method
System.out.println("queue contains Number 78249 : "
+ response1);
// check whether queue contains 9876
boolean response2 = queue.contains(9876);
// print after applying contains method
System.out.println("queue contains Number 9876: "
+ response2);
}
}
輸出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] queue contains Number 78249 : true queue contains Number 9876: false
相關用法
- Java ConcurrentLinkedQueue add()用法及代碼示例
- Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
- Java ConcurrentLinkedQueue iterator()用法及代碼示例
- Java ConcurrentLinkedQueue spliterator()用法及代碼示例
- Java ConcurrentLinkedQueue offer()用法及代碼示例
- Java ConcurrentLinkedQueue toArray()用法及代碼示例
- Java ConcurrentLinkedQueue addAll()用法及代碼示例
- Java ConcurrentLinkedQueue peek()用法及代碼示例
- Java ConcurrentLinkedQueue remove()用法及代碼示例
- Java ConcurrentLinkedQueue size()用法及代碼示例
- Java ConcurrentLinkedQueue poll()用法及代碼示例
- Java ConcurrentLinkedQueue用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ConcurrentLinkedQueue contains() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。