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

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


Posted in : Core Java Posted on : February 5, 2011 at 6:47 PM Comments : [ 0 ]

boolean removeLastOccurrence(Object o) This method deletes the last occurrence of a specific element from the deque.

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

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

Syntax

boolean removeLastOccurrence(Object o) 

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

In this method last occurrence of a specific element is to be deleted from deque incase of more than one such duplicate elements, if there is no duplicity exists in deque it removes that particular element. The deque remains unchanged if the specified element does not exist and returns false otherwise, returns true. 

Parameter description

o : It takes an element what do you want to remove from the deque.

Example of removeLastOccurrence(Object o) method

In this example we will show you how does removeLastOccurrence(Object o) method work in Deque interface. Through this example we will show how you can delete the last occurrence of the specified element from the underlying deque, and count the total number of elements in deque before and after removing the elements.

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
public class DequeRemoveLastOccurrence {
  public static void main(String[] args) {
    Deque dq = new LinkedList();
    dq.add(1);
    dq.add(2);
    dq.add(3);
    dq.add(4);
    dq.add(3);
    dq.add(5);
    System.out.println("Elements of deque : " + dq);
    System.out.println("Size of deque before removing element : "+dq.size());
    // Implementation of the method
    boolean bol = dq.removeLastOccurrence(3);
    if (bol == true) {
      System.out.println("The element is matched and ");
      System.out.println("removed it from deque then new deque = " + dq);
      System.out.println("Element is Removed = " + bol);
      System.out.println("Size of deque = " + dq.size());
    else {
      System.out.println("The element did not matched ");
      System.out.println("therefore deque remains unchanged " + dq);
      System.out.println("Element is Removed = " + bol);
      System.out.println("Size of deque = " + dq.size());
    }
  }
}

Output :

Elements of deque : [1, 2, 3, 4, 3, 5]

Size of deque before removing element : 6

The element is matched and

removed it from deque then new deque = [1, 2, 3, 4, 5]

Element is Removed = true

Size of deque = 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics