當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Deque contains()用法及代碼示例


Deque接口的contains(E e)方法檢查Deque容器中元素e的存在。如果雙端隊列包含元素的一次出現,則返回true,否則返回false。

用法:

boolean contains(Object o)

參數:此方法接受強製性參數o,該參數是是否需要在Deque中進行測試的元素。


返回值:如果元素存在於雙端隊列中,則該方法返回True,否則返回False。

異常:該函數引發兩個異常,如下所示:

  • ClassCastException–如果指定元素的類型與此雙端隊列不兼容。它是可選的。
  • NullPointerException –如果指定的元素為null,並且此雙端隊列不允許使用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<String> de_que = new LinkedList<String>(); 
  
        // 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

示例2:

// 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<Integer> de_que = new LinkedList<Integer>(); 
  
        // 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<Integer> de_que = new ArrayDeque<Integer>(); 
  
        // 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<Integer> de_que = new ConcurrentLinkedDeque<Integer>(); 
  
        // 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<Integer> de_que = new LinkedBlockingDeque<Integer>(); 
  
        // 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://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#contains-java.lang.Object-



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque contains() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。