Java ConcurrentLinkedQueue contains()方法
ConcurrentLinkedQueue 的 contains() 方法如果ConcurrentLinkedQueue包含作为参数传递的对象o,则返回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
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#contains-java.lang.Object-