ArrayDeque contains() Method in Java

The Java.util.ArrayDeque.contains() method in Java is used to check or verify whether a specific element is present in the Deque or not.

Syntax:

Array_Deque.contains(Object element)

Parameters: The parameter element is of the type of ArrayDeque. This 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.

Below programs illustrate the Java.util.ArrayDeque.contains() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate contains()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<String> de_que = new ArrayDeque<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 ArrayDeque
        System.out.println("ArrayDeque: " + 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"));
    }
}

chevron_right


Output:

ArrayDeque: [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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate contains()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        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 ArrayDeque
        System.out.println("ArrayDeque: " + 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));
    }
}

chevron_right


Output:

ArrayDeque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.