Deque接口的contains(E e)方法检查Deque容器中元素e的存在。如果双端队列包含元素的一次出现,则返回true,否则返回false。
用法:
boolean contains(Object o)
参数:此方法接受强制性参数o,该参数是是否需要在Deque中进行测试的元素。
返回值:如果元素存在于双端队列中,则该方法返回True,否则返回False。
异常:该函数引发两个异常,如下所示:
- ClassCastException–如果指定元素的类型与此双端队列不兼容。它是可选的。
- NullPointerException –如果指定的元素为null,并且此双端队列不允许使用null元素(可选)。它是可选的。
以下程序说明了Java中的contains()方法:
示例1:借助LinkedList。
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque<String> de_que = new LinkedList<String>();
// Use add() method to add elements into the Queue
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for "Geeks" in the deque
System.out.println("Does the deque contains 'Geeks'? "
+ de_que.contains("Geeks"));
// Check for "4" in the deque
System.out.println("Does the deque contains '4'? "
+ de_que.contains("4"));
// Check if the deque contains "No"
System.out.println("Does the deque contains 'No'? "
+ de_que.contains("No"));
}
}
输出:
Deque: [Welcome, To, Geeks, 4, Geeks] Does the deque contains 'Geeks'? true Does the deque contains '4'? true Does the deque contains 'No'? false
示例2:
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque<Integer> de_que = new LinkedList<Integer>();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
输出:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
示例3:借助ArrayDeque。
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque<Integer> de_que = new ArrayDeque<Integer>();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
输出:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
示例4:借助ConcurrentLinkedDeque。
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque<Integer> de_que = new ConcurrentLinkedDeque<Integer>();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
输出:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
示例5:借助LinkedBlockingDeque。
// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String args[])
{
// Creating an empty Deque
Deque<Integer> de_que = new LinkedBlockingDeque<Integer>();
// Use add() method to add elements into the Queue
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the Deque
System.out.println("Deque: " + de_que);
// Check for '15' in the Deque
System.out.println("Does the Deque contains '15'? "
+ de_que.contains(15));
// Check for '2' in the Deque
System.out.println("Does the Deque contains '2'? "
+ de_que.contains(2));
// Check if the Deque contains '10'
System.out.println("Does the Deque contains '10'? "
+ de_que.contains(10));
}
}
输出:
Deque: [10, 15, 30, 20, 5] Does the Deque contains '15'? true Does the Deque contains '2'? false Does the Deque contains '10'? true
注意:这些异常取决于编译器,因此无法在程序中显示。
参考: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#contains-java.lang.Object-
相关用法
- Java Deque add()用法及代码示例
- Java Deque iterator()用法及代码示例
- Java Deque offer()用法及代码示例
- Java Deque addLast()用法及代码示例
- Java Deque offerLast()用法及代码示例
- Java Deque offerFirst()用法及代码示例
- Java Deque getLast()用法及代码示例
- Java Deque getFirst()用法及代码示例
- Java Deque element()用法及代码示例
- Java Deque descendingIterator()用法及代码示例
- Java Deque addFirst()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Deque contains() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。