El método contains(E e) de la interfaz Deque verifica la presencia del elemento e en el contenedor Deque. Si el Deque contiene una aparición del elemento, devuelve verdadero; de lo contrario, devuelve falso.
Sintaxis:
boolean contains(Object o)
Parámetros: Este método acepta un parámetro obligatorio o cual es el elemento que se necesita probar si está presente en el Deque o no.
Valor devuelto: el método devuelve True si el elemento está presente en Deque; de lo contrario, devuelve False.
Excepciones: la función lanza dos excepciones como se muestra a continuación:
- ClassCastException : si el tipo del elemento especificado es incompatible con esta deque. es opcional
- NullPointerException : si el elemento especificado es nulo y este Deque no permite elementos nulos (opcional). es opcional
Los siguientes programas ilustran el método contains() en Java:
Programa 1: Con la ayuda de 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
Programa 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
Programa 3: Con la ayuda de 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
Programa 4: Con la ayuda de 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
Programa 5: Con la ayuda de 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
Nota: Las excepciones dependen del compilador, por lo que no se pueden mostrar en el programa.
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#contains-java.lang.Object-