Home »
Java programming language
Java ArrayDeque contains() Method with Example
ArrayDeque Class contains() method: Here, we are going to learn about the contains() method of ArrayDeque Class with its syntax and example.
Submitted by Preeti Jain, on January 09, 2020
ArrayDeque Class contains() method
- contains() Method is available in java.lang package.
- contains() Method is used to check whether the given element exists or not exists in the deque.
- contains() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- contains() Method does not throw an exception at the time of checking the given element that exists in the deque.
Syntax:
public boolean contains(Object obj);
Parameter(s):
- Object obj – represents the element to be checked as existence in deque.
Return value:
The return type of the method is boolean, it returns true if the given element exists in this deque, else it returns false.
Example:
// Java program to demonstrate the example
// of boolean contains(Object obj) method of ArrayDeque
import java.util.*;
public class ContainsOfArrayDeque {
public static void main(String[] args) {
// Creating an ArrayDeque with initial capacity of
// storing elements
ArrayDeque < String > d_queue = new ArrayDeque < String > (10);
// By using add() method to add elements
// in ArrayDeque
d_queue.add("C");
d_queue.add("C++");
d_queue.add("Java");
d_queue.add("Php");
d_queue.add("DotNet");
// Display Deque Elements
System.out.println("d_queue : ");
System.out.println("ArrayDeque Elements = " + d_queue);
// By using contains() method to check the existing
// element in ArrayDeque
boolean b = d_queue.contains("Java");
System.out.println();
// Display Deque Elements
System.out.println("d_queue.contains Java: ");
System.out.println("Is d_queue contains element :: java - " + b);
}
}
Output
d_queue :
ArrayDeque Elements = [C, C++, Java, Php, DotNet]
d_queue.contains Java:
Is d_queue contains element :: java - true