Java Deque contains()方法
Deque接口的 contains(E e) 方法检查Deque容器中是否存在元素e。如果Deque中包含一个元素,那么它就会返回true,否则就会返回false。
语法
boolean contains(Object o)
参数: 该方法接受一个强制参数o,即需要测试的元素是否存在于Deque中。
返回值: 如果该元素存在于Deque中,该方法返回True,否则返回False。
异常: 该函数抛出两个异常,如下所示。
- ClassCastException – 如果指定元素的类型与这个Deque不兼容。它是可选的。
- NullPointerException – 如果指定的元素是空的,而这个Deque不允许空元素(可选)。它是可选的。
下面的程序说明了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-