Java ArrayDeque contains(Object) Method



Description

The Java ArrayDeque contains(Object) returns true if this deque contains the specified element. In case of custom objects, equals() methd should be implemented so that contains can compare objects correctly.

Declaration

Following is the declaration for java.util.ArrayDeque.contains() method

public boolean contains(Object o) 

Parameters

o − The object to be checked as contained in deque.

Return Value

This method returns true if this deque contains the specified element, else false.

Exception

NA

Example 1

The following example shows the usage of Java ArrayDeque contains() method. We're using Integers. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(20);
      deque.add(30);
      deque.add(10);
      deque.add(18);        

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // deque contains element 10
      if (deque.contains(10)) {
         System.out.println("element 10 is present in the deque");
      } else {
         System.out.println("element 10 is not present in the deque");
      }

      // deque does not contain element 25
      if (deque.contains(25)) {
         System.out.println("element 25 is present in the deque");
      } else {
         System.out.println("element 25 is not present in the deque");    
      }
   }
}           

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [20, 30, 10, 18]
element 10 is present in the deque
element 25 is not present in the deque

Example 2

The following example shows the usage of Java ArrayDeque contains() method with Strings. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("Welcome");
      deque.add("to");
      deque.add("tutorialspoint");
      deque.add(".com");        

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // deque contains element tutorialspoint
      if (deque.contains("tutorialspoint")) {
         System.out.println("element tutorialspoint is present in the deque");
      } else {
         System.out.println("element tutorialspoint is not present in the deque");
      }

      // deque does not contain element html
      if (deque.contains("html")) {
         System.out.println("element html is present in the deque");
      } else {
         System.out.println("element html is not present in the deque");    
      }
   }
}           

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [Welcome, to, tutorialspoint, .com]
element tutorialspoint is present in the deque
element html is not present in the deque

Example 3

The following example shows the usage of Java ArrayDeque contains() method with Student objects. We'll be adding few elements and then checking if certain element is present or not.

package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));      

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // deque contains element Robert
      if (deque.contains(new Student(2, "Robert"))) {
         System.out.println("Student Robert is present in the deque");
      } else {
         System.out.println("Student Robert is not present in the deque");
      }

      // deque does not contain element Jane
      if (deque.contains(new Student(4, "Jane"))) {
         System.out.println("Student Jane is present in the deque");
      } else {
         System.out.println("Student Jane is not present in the deque");    
      }
   }
} 
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student Robert is present in the deque
Student Jane is not present in the deque
java_util_arraydeque.htm
Advertisements