Java Utility Library

Java ArrayDeque - contains() Method



The java.util.ArrayDeque.contains() method is used to check whether the deque contains the specified element or not. It returns true if the deque contains the specified element, else returns false.

Syntax

public boolean contains(Object obj)

Parameters

obj Specify element whose presence in the deque need to be tested.

Return Value

Returns true if the deque contains the specified element, else returns false.

Exception

NA

Example:

In the example below, the java.util.ArrayDeque.contains() method is used to check the presence of specified element in the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);

    //checking the presence of elements
    for(int i = 5; i <= 20; i += 5) {
      if(MyDeque.contains(i))
        System.out.println(i +" is present in MyDeque.");
      else
        System.out.println(i +" is NOT present in MyDeque.");
    }       
  }
}

The output of the above code will be:

5 is NOT present in MyDeque.
10 is present in MyDeque.
15 is NOT present in MyDeque.
20 is present in MyDeque.

❮ Java.util - ArrayDeque