Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误和异常

Java 多线程

Java 同步

Java 网络编程

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java APIs 和框架

Java 类引用

Java 有用资源

Java - Deque contains(Object) 方法



Java Deque contains(Object) 方法返回 true,如果此 deque 包含指定的元素。对于自定义对象,应实现 equals() 方法,以便 contains 可以正确比较对象。

声明

以下是 java.util.Deque.contains() 方法的声明

public boolean contains(Object o) 

参数

o − 要检查是否包含在 deque 中的对象。

返回值

如果此 deque 包含指定的元素,则此方法返回 true,否则返回 false

异常

ClassCastException − 如果指定元素的类与此 deque 不兼容(可选)

NullPointerException − 如果指定元素为 null,并且此 deque 不允许 null 元素(可选)

示例 1

以下示例显示了 Java Deque contains() 方法的用法。我们使用的是 Integer。我们将添加一些元素,然后检查某些元素是否存在。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   public static void main(String[] args) {

      // create an empty 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("Deque = " + 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");    
      }
   }
}           

输出

让我们编译并运行上面的程序,这将产生以下结果:

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

示例 2

以下示例显示了 Java Deque contains() 方法与字符串一起使用的用法。我们将添加一些元素,然后检查某些元素是否存在。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   public static void main(String[] args) {

      // create an empty 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("Deque = " + 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");    
      }
   }
}           

输出

让我们编译并运行上面的程序,这将产生以下结果:

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

示例 3

以下示例显示了 Java Deque contains() 方法与 Student 对象一起使用的用法。我们将添加一些元素,然后检查某些元素是否存在。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeDemo {
   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("Deque = " + 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);
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

Deque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student Robert is present in the deque
Student Jane is not present in the deque
java_util_deque.htm
广告