Java中的 ConcurrentLinkedDeque contains() 方法及示例
Java.util.concurrent.ConcurrentLinkedDeque.contains() 方法是Java中的一个内置方法,它检查作为参数传递的指定元素是否存在于双端队列中。
句法:
public boolean contains(Object elem)
参数:该方法接受一个参数elem ,它检查指定的元素是否存在于双端队列中。
返回值:如果元素存在于双端队列中,该函数返回True ,否则返回False 。
下面的程序说明了ConcurrentLinkedDeque.contains()方法:
程序 1 :
// Java Program to Demonstrate contains()
// method of ConcurrentLinkedDeque
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque cld = new ConcurrentLinkedDeque();
// Add elements into the Deque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the Deque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
// Check if GFG is present using contains()
if (cld.contains("GFG")) {
// Displaying message
System.out.println("GFG is present");
}
else {
// Displaying message
System.out.println("GFG is not present");
}
}
}
输出:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
GFG is not present
方案二:
// Java Program to Demonstrate contains()
// method of ConcurrentLinkedDeque
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Creating an empty Deque
ConcurrentLinkedDeque cld = new ConcurrentLinkedDeque();
// Add elements into the Deque
cld.add("Welcome");
cld.add("To");
cld.add("Geeks");
cld.add("4");
cld.add("Geeks");
// Displaying the Deque
System.out.println("ConcurrentLinkedDeque: "
+ cld);
// Check if Geeks is present using contains()
if (cld.contains("Geeks")) {
// Displaying message
System.out.println("Geeks is present");
}
else {
// Displaying message
System.out.println("Geeks not present");
}
}
}
输出:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
Geeks is present
参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/concurrent/ConcurrentLinkedDeque.html#contains-java.lang.Object-