Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ArrayDeque.contains method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

An ArrayDequeArray Double Ended Queue is a growable array that allows us to add or remove an element from both the front and back.

This class is the implementation class of the Deque interface. ArrayDeque can be used as both a Stack or Queue.

Null elements are prohibited.

What is the contains method of the ArrayDeque?

The contains method can be used to check if the specified element is present in the ArrayDeque object.

Syntax


public boolean contains(Object o)

Parameters

This method takes the element to be checked as a parameter.

Return value

This method returns true if the passed element is present in the deque. Otherwise, it returns false.

Code

The code below demonstrates how to use the contains method.

import java.util.ArrayDeque;
class Contains{
    public static void main( String args[] ) {
      ArrayDeque<String> arrayDeque = new ArrayDeque<>();
      arrayDeque.add("1");
      arrayDeque.add("2");
      arrayDeque.add("3");
      System.out.println("The arrayDeque is " + arrayDeque);
      
      System.out.println("Check if 1 present in the arrayDeque : " + arrayDeque.contains("1"));
      System.out.println("Check if 4 present in the arrayDeque : " + arrayDeque.contains("4"));
    }
}

Explanation

In the above code:

  • In line 1, we import the ArrayDeque class.

  • In line 4, we create an ArrayDeque object with the name arrayDeque.

  • In line 5 to 7, we use the add() method of the arrayDeque object to add three elements ("1","2","3") to deque.

  • In line 10, we use the contains() method of the arrayDeque object to check if the element "1" is present.

    In our case, true will be returned because "1" is available in the arrayDeque object.

  • In line 11, we use the contains() method of the arrayDeque object to check if the element "4" is present.

    In our case, false will be returned because "4" is not available in the arrayDeque object.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring