📜  Java中的双端队列 contains() 方法

📅  最后修改于: 2022-05-13 01:55:00.187000             🧑  作者: Mango

Java中的双端队列 contains() 方法

Deque 接口的contains(E e)方法检查 Deque 容器中是否存在元素 e。如果双端队列包含一个元素,则返回 true,否则返回 false。

句法:

boolean contains(Object o)

参数:此方法接受一个强制参数 o,它是需要测试的元素是否存在于 Deque 中。

返回值:如果元素存在于 Deque 中,则该方法返回True ,否则返回False。

异常:该函数抛出两个异常,如下所示:

  • ClassCastException – 如果指定元素的类型与此双端队列不兼容。它是可选的。
  • NullPointerException – 如果指定元素为 null 并且此 Deque 不允许 null 元素(可选)。它是可选的。

下面的程序说明了Java中的 contains() 方法:

程序 1:LinkedList的帮助下。

// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedList();
  
        // Use add() method to add elements into the Queue
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Displaying the Deque
        System.out.println("Deque: " + de_que);
  
        // Check for "Geeks" in the deque
        System.out.println("Does the deque contains 'Geeks'? "
                           + de_que.contains("Geeks"));
  
        // Check for "4" in the deque
        System.out.println("Does the deque contains '4'? "
                           + de_que.contains("4"));
  
        // Check if the deque contains "No"
        System.out.println("Does the deque contains 'No'? "
                           + de_que.contains("No"));
    }
}
输出:
Deque: [Welcome, To, Geeks, 4, Geeks]
Does the deque contains 'Geeks'? true
Does the deque contains '4'? true
Does the deque contains 'No'? false

方案二:

// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedList();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the Deque
        System.out.println("Deque: " + de_que);
  
        // Check for '15' in the Deque
        System.out.println("Does the Deque contains '15'? "
                           + de_que.contains(15));
  
        // Check for '2' in the Deque
        System.out.println("Does the Deque contains '2'? "
                           + de_que.contains(2));
  
        // Check if the Deque contains '10'
        System.out.println("Does the Deque contains '10'? "
                           + de_que.contains(10));
    }
}
输出:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

程序 3:ArrayDeque的帮助下。

// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new ArrayDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the Deque
        System.out.println("Deque: " + de_que);
  
        // Check for '15' in the Deque
        System.out.println("Does the Deque contains '15'? "
                           + de_que.contains(15));
  
        // Check for '2' in the Deque
        System.out.println("Does the Deque contains '2'? "
                           + de_que.contains(2));
  
        // Check if the Deque contains '10'
        System.out.println("Does the Deque contains '10'? "
                           + de_que.contains(10));
    }
}
输出:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

程序 4:ConcurrentLinkedDeque的帮助下。

// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new ConcurrentLinkedDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the Deque
        System.out.println("Deque: " + de_que);
  
        // Check for '15' in the Deque
        System.out.println("Does the Deque contains '15'? "
                           + de_que.contains(15));
  
        // Check for '2' in the Deque
        System.out.println("Does the Deque contains '2'? "
                           + de_que.contains(2));
  
        // Check if the Deque contains '10'
        System.out.println("Does the Deque contains '10'? "
                           + de_que.contains(10));
    }
}
输出:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

程序 5:LinkedBlockingDeque的帮助下。

// Java code to illustrate contains()
// method of Deque in Java
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String args[])
    {
        // Creating an empty Deque
        Deque de_que = new LinkedBlockingDeque();
  
        // Use add() method to add elements into the Queue
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the Deque
        System.out.println("Deque: " + de_que);
  
        // Check for '15' in the Deque
        System.out.println("Does the Deque contains '15'? "
                           + de_que.contains(15));
  
        // Check for '2' in the Deque
        System.out.println("Does the Deque contains '2'? "
                           + de_que.contains(2));
  
        // Check if the Deque contains '10'
        System.out.println("Does the Deque contains '10'? "
                           + de_que.contains(10));
    }
}
输出:
Deque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true

注意:异常取决于编译器,因此无法在程序中显示。
参考: https: Java/util/Deque.html#contains-java.lang.Object-