removeLastOccurrence(Object o) method of BlockingDeque Interface in Java.

removeLastOccurrence(Object o) method of BlockingDeque Interface in Java.


Posted in : Core Java Posted on : March 4, 2011 at 6:14 PM Comments : [ 0 ]

It deletes the last occurrence of the specified element from a deque if the specified element present into the deque more than one times otherwise, it removes that particular single element. There is no changes happened in deque if the specified element is not present in it and returns 'false' otherwise, returns 'true'.� 

removeLastOccurrence(Object o) method of BlockingDeque Interface in Java.

In this section we will discuss how can removeLastOccurrence(Object o) method be implemented in BlockingDeque interface in java.

Syntax

boolean removeLastOccurrence(Object o) 

This method deletes the last occurrence of the specified element from a deque.

It deletes the last occurrence of the specified element from a deque if the specified element present into the deque more than one times otherwise, it removes that particular single element. There is no changes happened in deque if the specified element is not present in it and returns 'false' otherwise, returns 'true'. 

Parameter description

o : It takes an element of which do you want to remove the last occurrence of the specified element from the deque.

Example of removeLastOccurrence(Object o) method

In this example we will show you how does removeLastOccurrence(Object o) method work in BlockingDeque interface. This example will help you to understand how can you remove the last occurrence of the specified element from the underlying deque.

Example :

package devmanuals.com;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
class RemoveLastOccurrence implements Runnable {
  BlockingDeque<Integer> bdq;
  String name;
  public RemoveLastOccurrence(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(2);
    System.out.println(name" adds the new element into the deque then new deque = "+ bdq);
    System.out.println("Size of deque = " + bdq.size());
    bdq.removeLastOccurrence(2);
    System.out.println("Remaining element of deque after removing the "+"\n last occurrence of '2' = "+ bdq);
  }
}
class RemoveLastOccurrence1 implements Runnable {
  String name;
  BlockingDeque<Integer> bdq;
  public RemoveLastOccurrence1(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 BdqRemoveLastOccurrence {
  public static void main(String args[]) {
    BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(5);
    Runnable a = new RemoveLastOccurrence("A", bdq);
    new Thread(a).start();
    try {
      Thread.sleep(400);
    catch (InterruptedException e) {
      e.printStackTrace();
    }
    Runnable b = new RemoveLastOccurrence1("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, 2]

Size of deque = 5

Remaining element of deque after removing the

last occurrence of '2' = [1, 2, 3, 4]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics