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


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


BlockingDeque的contains(Object o)方法检查参数中传递的元素是否在容器中。如果该元素存在于容器中,则返回true,否则返回false值。

用法:

public boolean contains(Object o)

参数:此方法接受一个强制性参数o,该参数要检查容器中是否存在容器。


返回值:如果该元素存在,则此方法返回true,否则返回false。

注意:BlockingDeque的contains()方法已从Java中的LinkedBlockingDeque类继承。

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

示例1:

// Java Program Demonstrate contains() 
// method of BlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.concurrent.BlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of BlockingDeque 
        BlockingDeque<Integer> BD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of BlockingDeque 
        BD.add(10); 
        BD.add(20); 
        BD.add(30); 
        BD.add(40); 
  
        // before removing print Deque 
        System.out.println("Blocking Deque: " + BD); 
  
        // check for presence using function 
        if (BD.contains(10)) { 
            System.out.println("Blocking Deque contains 10"); 
        } 
        else { 
            System.out.println("Blocking Deque does not contain 10"); 
        } 
    } 
}
输出:
Blocking Deque: [10, 20, 30, 40]
Blocking Deque contains 10

示例2:

// Java Program Demonstrate contains() 
// method of BlockingDeque 
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.concurrent.BlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of BlockingDeque 
        BlockingDeque<String> BD 
            = new LinkedBlockingDeque<String>(); 
  
        // Add numbers to end of BlockingDeque 
        BD.add("ab"); 
        BD.add("cd"); 
        BD.add("fg"); 
        BD.add("xz"); 
  
        // before removing print Deque 
        System.out.println("Blocking Deque: " + BD); 
  
        // check for presence using function 
        if (BD.contains("go")) { 
            System.out.println("Blocking Deque contains 'go'"); 
        } 
        else { 
            System.out.println("Blocking Deque does not contain 'go'"); 
        } 
    } 
}
输出:
Blocking Deque: [ab, cd, fg, xz]
Blocking Deque does not contain 'go'

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#contains(java.lang.Object)



相关用法


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