Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Deque descendingIterator() Method in Java
Next article icon

Deque contains() method in Java

Last Updated : 21 Sep, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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
// 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
// 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
// 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
// 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
// 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-

Next Article
Deque descendingIterator() Method in Java

G

gopaldave
Improve
Article Tags :
  • Misc
  • Java
  • Java - util package
  • Java-Functions
  • java-deque
Practice Tags :
  • Java
  • Misc

Similar Reads

    Deque Interface in Java
    Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/
    9 min read
    Deque add() method in Java
    The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:   bool
    4 min read
    Deque addFirst() method in Java with Examples
    The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:
    4 min read
    Deque addLast() method in Java
    The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: bo
    4 min read
    Deque contains() method in Java
    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 whic
    5 min read
    Deque descendingIterator() Method in Java
    The descendingIterator() method of the Deque Interface returns an iterator over the elements in this deque in reverse order. The elements will be returned from the last to the first order. This can be used with any class implementing the Deque Interface like LinkedList, ArrayDeque, LinkedBlockingDeq
    2 min read
    Deque element() method in Java
    The element() method of Deque Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the Deque. The method throws an exception when the Deque is empty. Syntax:   E element() Parameters: This method does not accepts
    3 min read
    Deque getFirst() method in Java
    The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax:   E getFirst() Parameters: This method does not accepts any parameter.Returns: This method returns the first element o
    3 min read
    Deque getLast() method in Java
    The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax: E getLast() Parameters: This method does not accepts any parameter. Returns: This method returns the last element or the
    3 min read
    Deque iterator() method in Java
    The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator. Syntax: Iterator iterator() Parameters: This method doe
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences