In this method you can test the presence of the specified element into the underlying deque. Generally, this method returns true if and only if at least there is one
contains(Object o) method of BlockingDeque Interface in Java.
In this section we will discuss how can contains(Object o) method be implemented in BlockingDeque interface in java.
Syntax
boolean contains(Object o)
This method returns 'true' if the deque has present the specified element otherwise, returns 'false'.
In this method you can test the presence of the specified element into the underlying deque. Generally, this method returns true if and only if at least there is one element is contained by the underlying deque.
Parameter description
Object o : It takes an element to test for the presence into the underlying deque.
Example of contains(Object o) method
In this example we will show you how does contains(Object o) method work in BlockingDeque interface. This example will help you to understand how can you test for the containment of the specified element into the underlying deque. Further we will see after using the contains() method what it returns.
Example :
package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; class Contains implements Runnable { BlockingDeque<Integer> bdq; public Contains(BlockingDeque<Integer> bdq) { this.bdq = bdq; } public void run() { System.out.println("Elements of deque are : "); int i; boolean bol=false; try{ for(i=0; i<5;i++){ Thread.sleep(200); bdq.add(i); } System.out.println(bdq); System.out.println("Size of deque = "+bdq.size()); }catch(InterruptedException e){ e.printStackTrace(); } try { Thread.sleep(1000); System.out.println("Is element '4' is present in deque? "); Thread.sleep(1000); bol= bdq.contains(4); System.out.println(bol); } catch (InterruptedException e){ e.printStackTrace(); } } } public class BdqContains { public static void main(String[] args) { BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(); Runnable a = new Contains(bdq); new Thread(a).start(); } }
Output :
Elements of deque are : [0, 1, 2, 3, 4] Size of deque = 5 Is element '4' is present in deque? true |
[ 0 ] Comments