Java中的 BlockingDeque contains() 方法及示例
BlockingDeque的contains(Object o)方法检查参数中传递的元素是否存在于容器中。如果元素存在于容器中,则返回 true,否则返回 false 值。
句法:
public boolean contains(Object o)
参数:此方法接受一个强制参数o ,它在容器中的存在将在容器中检查。
返回:如果元素存在,此方法返回 true,否则返回 false。
注意: BlockingDeque的 contains() 方法继承自Java中的 LinkedBlockingDeque 类。
下面的程序说明了 BlockingDeque 的 contains() 方法:
方案一:
// Java Program Demonstrate contains()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque BD
= new LinkedBlockingDeque();
// Add numbers to end of BlockingDeque
BD.add(10);
BD.add(20);
BD.add(30);
BD.add(40);
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
// check for presence using function
if (BD.contains(10)) {
System.out.println("Blocking Deque contains 10");
}
else {
System.out.println("Blocking Deque does not contain 10");
}
}
}
输出:
Blocking Deque: [10, 20, 30, 40]
Blocking Deque contains 10
方案二:
// Java Program Demonstrate contains()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque BD
= new LinkedBlockingDeque();
// Add numbers to end of BlockingDeque
BD.add("ab");
BD.add("cd");
BD.add("fg");
BD.add("xz");
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
// check for presence using function
if (BD.contains("go")) {
System.out.println("Blocking Deque contains 'go'");
}
else {
System.out.println("Blocking Deque does not contain 'go'");
}
}
}
输出:
Blocking Deque: [ab, cd, fg, xz]
Blocking Deque does not contain 'go'
参考: https: Java Java.lang.Object)