boolean remove(Object o) This method deletes the specified element from the deque.
remove(o) method of BlockingDeque Interface in Java.
In this section we will discuss how can remove(o) method be implemented in BlockingDeque interface in java.
Syntax
boolean remove(Object o)
This method deletes the specified element from the deque.
In this method the first occurrence of a defined element is to be removed from the underlying deque. In deque there will not be changes happened if the specified element does not present in it. Generally at first it matches the specified element with the other element into the deque, if the element matches it removes the first occurrence of that element and returns 'true' otherwise, it does not put any effect onto the deque and return 'false'. This method is similar to the removeFirstOccurrence(Object o).
Parameter description
o : It takes an object what do you want to remove its first occurrence from the underlying deque.
Example of remove(Object o) method
In this example we will show you how does remove(o) method work in BlockingDeque interface. This example will help you to understand how can you remove the first occurrence of a specified element from the underlying deque.
Example :
package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; class RemoveObject implements Runnable { BlockingDeque<Integer> bdq; String name; public RemoveObject(String name, BlockingDeque<Integer> bdq) { this.bdq = bdq; this.name = name; } public void run() { int i; System.out.println(name + " inserted the element into the deque"); for (i = 0; i < 5; i++) { bdq.add(i); System.out.println(i); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } bdq.add(3); System.out.println(name+ " adds the new element into the deque then new deque = "+ bdq); System.out.println("Size of deque = " + bdq.size()); bdq.remove(3); System.out.println("Remaining element of deque after removing the "+"\n first occurrence of '3' = "+ bdq); } } class RemoveObject1 implements Runnable { String name; BlockingDeque<Integer> bdq; public RemoveObject1(String name, BlockingDeque<Integer> bdq) { this.bdq = bdq; this.name = name; } public void run() { try { bdq.remove(); Thread.sleep(1500); System.out.println(name+ " deleted the first element then remaining deque = "+ bdq); System.out.println("Size of deque = " + bdq.size()); } catch (InterruptedException e) { e.printStackTrace(); } } } public class BdqRemoveObject { public static void main(String args[]) { BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5); Runnable a = new RemoveObject("A", bdq); new Thread(a).start(); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } Runnable b = new RemoveObject1("B", bdq); new Thread(b).start(); } }
Output :
A inserted the element into the deque 0 1 2 3 4 B deleted the first element then remaining deque = [1, 2, 3, 4] Size of deque = 4 A adds the new element into the deque then new deque = [1, 2, 3, 4, 3] Size of deque = 5 Remaining element of deque after removing the first occurrence of '3' = [1, 2, 4, 3] |
[ 0 ] Comments