Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the Deque.contains() method in Java?

Sumit Sharma

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.

In this shot, we will discuss how to use the Deque.contains() method in Java.

The Deque.contains() method is present in the Deque interface inside the java.util package. It is used to check the presence of the particular element in the deque.

Syntax

boolean contains(element)

Parameter

The Deque.contains() takes 1 parameter.

  • Element: Element to be checked.

Return

The Deque.contains() method returns boolean value:

  • True: When the element is found in the deque.
  • False: When the element is not found in the deque.

Code

Let’s look at the code snippet below:

import java.util.*;
class Main
{
public static void main(String[] args)
{
Deque<Integer> d = new LinkedList<Integer>();
d.add(212);
d.add(621);
d.add(23);
d.add(1280);
d.add(1171);
System.out.println("Elements in the deque are: ");
System.out.println("Element 23 is found?: "+d.contains(23));
System.out.println("Element 253 is found?: "+d.contains(253));
System.out.println("Element 1171 is found?: "+d.contains(1171));
}
}
Using Deque.contains() method in Java

Explanation

  • In line 1, we imported the required package.
  • In line 2, we made the Main class.
  • In line 4, we made the main function.
  • In line 6, we declared a deque consisting of Integer type elements.
  • From lines 8 to 12, we inserted elements in the deque using the Deque.add() method.
  • In line 14, we displayed the original deque with a message.
  • In lines 15 to 17, we checked the presence of elements in the deque with the help of Deque.contains() method and displayed the result with a message.

In this way, we can use Deque.contains() method in Java.

RELATED TAGS

communitycreator
java

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