Skip to content
Related Articles

Related Articles

Deque contains() method in Java
  • Last Updated : 21 Sep, 2018

The contains(E e) method of Deque Interface check for the presence of the element e in the Deque container. If the Deque contains one occurrence of the element, then it returns true else it returns false.

Syntax:

boolean contains(Object o)

Parameters: This method accepts a mandatory parameter o which is the element that needs to be tested if it is present in the Deque or not.

Return Value: The method returns True if the element is present in the Deque otherwise it returns False.

Exceptions: The function throws two exceptions as shown below:



  • ClassCastException – if the type of the specified element is incompatible with this deque. It is optional.
  • NullPointerException – if the specified element is null and this Deque does not permit null elements (optional). It is optional.

Below programs illustrate the contains() method in Java:

Program 1: With the help of 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"));
    }
}
Output:
Deque: [Welcome, To, Geeks, 4, Geeks]
Does the deque contains 'Geeks'? true
Does the deque contains '4'? true
Does the deque contains 'No'? false

Program 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));
    }
}
Output:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

Program 3: With the help of 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));
    }
}
Output:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

Program 4: With the help of 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));
    }
}
Output:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

Program 5: With the help of 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));
    }
}
Output:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

Note: The exceptions are compiler dependent, hence cannot be shown in the program.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#contains-java.lang.Object-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :