BlockingQueue接口的contains(Object o)方法检查容器中是否存在参数中传递的元素。如果该元素存在于容器中,则返回true,否则返回false值。
用法:
public boolean contains(Object o)
参数:此方法接受一个强制性参数o,该参数要检查容器中是否存在容器。
返回值:如果该元素存在,则此方法返回true,否则返回false。
注意:BlockingQueue的contains()方法已从Java中的Queue类继承。
下面的程序说明BlockingQueue的contains()方法:
示例1:
// Java Program Demonstrate contains()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingQueue
BlockingQueue<Integer> BQ
= new LinkedBlockingQueue<Integer>();
// Add numbers to end of BlockingQueue
BQ.add(10);
BQ.add(20);
BQ.add(30);
BQ.add(40);
// before removing print queue
System.out.println("Blocking Queue: " + BQ);
// check for presence using function
if (BQ.contains(10)) {
System.out.println("Blocking Queue contains 10");
}
else {
System.out.println("Blocking Queue does not contain 10");
}
}
}
输出:
Blocking Queue: [10, 20, 30, 40] Blocking Queue contains 10
示例2:
// Java Program Demonstrate contains()
// method of BlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>();
// Add numbers to end of BlockingQueue
BQ.add("ab");
BQ.add("cd");
BQ.add("fg");
BQ.add("xz");
// before removing print queue
System.out.println("Blocking Queue: " + BQ);
// check for presence using function
if (BQ.contains("go")) {
System.out.println("Blocking Queue contains 'go'");
}
else {
System.out.println("Blocking Queue does not contain 'go'");
}
}
}
输出:
Blocking Queue: [ab, cd, fg, xz] Blocking Queue does not contain 'go'
相关用法
- Java BlockingQueue put()用法及代码示例
- Java BlockingQueue take()用法及代码示例
- Java BlockingQueue remove()用法及代码示例
- Java BlockingQueue drainTo()用法及代码示例
- Java BlockingQueue remainingCapacity()用法及代码示例
- Java BlockingQueue poll()用法及代码示例
- Java BlockingQueue offer()用法及代码示例
- Java BlockingQueue add()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingQueue contains() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。