当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ConcurrentLinkedDeque contains()用法及代码示例


java.util.concurrent.ConcurrentLinkedDeque.contains()方法是Java中的内置方法,用于检查双端队列中是否存在作为参数传递的指定元素。

用法:

public boolean contains(Object elem)

参数:该方法接受参数elem,该参数检查指定的元素是否存在于双端队列中。


返回值:如果双端队列中存在元素,则函数返回True,否则返回False。

以下示例程序旨在说明ConcurrentLinkedDeque.contains()方法:

程序1

// Java Program to Demonstrate contains() 
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty Deque 
        ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>(); 
  
        // Add elements into the Deque 
        cld.add("Welcome"); 
        cld.add("To"); 
        cld.add("Geeks"); 
        cld.add("4"); 
        cld.add("Geeks"); 
  
        // Displaying the Deque 
        System.out.println("ConcurrentLinkedDeque: "
                           + cld); 
  
        // Check if GFG is present using contains() 
        if (cld.contains("GFG")) { 
  
            // Displaying message 
            System.out.println("GFG is present"); 
        } 
        else { 
  
            // Displaying message 
            System.out.println("GFG is not present"); 
        } 
    } 
}
输出:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
GFG is not present

示例2:

// Java Program to Demonstrate contains() 
// method of ConcurrentLinkedDeque 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty Deque 
        ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>(); 
  
        // Add elements into the Deque 
        cld.add("Welcome"); 
        cld.add("To"); 
        cld.add("Geeks"); 
        cld.add("4"); 
        cld.add("Geeks"); 
  
        // Displaying the Deque 
        System.out.println("ConcurrentLinkedDeque: "
                           + cld); 
  
        // Check if Geeks is present using contains() 
        if (cld.contains("Geeks")) { 
  
            // Displaying message 
            System.out.println("Geeks is present"); 
        } 
        else { 
  
            // Displaying message 
            System.out.println("Geeks not present"); 
        } 
    } 
}
输出:
ConcurrentLinkedDeque: [Welcome, To, Geeks, 4, Geeks]
Geeks is present

参考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#contains-java.lang.Object-



相关用法


注:本文由纯净天空筛选整理自Aishwarya Jayashankar大神的英文原创作品 ConcurrentLinkedDeque contains() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。