BlockingDeque的contains(Object o)方法檢查參數中傳遞的元素是否在容器中。如果該元素存在於容器中,則返回true,否則返回false值。
用法:
public boolean contains(Object o)
參數:此方法接受一個強製性參數o,該參數要檢查容器中是否存在容器。
返回值:如果該元素存在,則此方法返回true,否則返回false。
注意:BlockingDeque的contains()方法已從Java中的LinkedBlockingDeque類繼承。
以下示例程序旨在說明BlockingDeque的contains()方法:
示例1:
// 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<Integer> BD
= new LinkedBlockingDeque<Integer>();
// 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
示例2:
// 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<String> BD
= new LinkedBlockingDeque<String>();
// 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'
相關用法
- Java BlockingDeque add()用法及代碼示例
- Java BlockingDeque take()用法及代碼示例
- Java BlockingDeque put()用法及代碼示例
- Java BlockingDeque size()用法及代碼示例
- Java BlockingDeque peek()用法及代碼示例
- Java BlockingDeque remove()用法及代碼示例
- Java BlockingDeque removeLastOccurrence()用法及代碼示例
- Java BlockingDeque iterator()用法及代碼示例
- Java BlockingDeque takeFirst()用法及代碼示例
- Java BlockingDeque element()用法及代碼示例
- Java BlockingDeque offerFirst()用法及代碼示例
- Java BlockingDeque offerLast()用法及代碼示例
- Java BlockingDeque addLast()用法及代碼示例
- Java BlockingDeque addFirst()用法及代碼示例
- Java BlockingDeque pollFirst()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingDeque contains() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。