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

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


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

boolean removeFirstOccurrence(Object o)� This method deletes the first occurrence of a specific element from a deque.

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

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

Syntax

boolean removeFirstOccurrence(Object o) 

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

In this method a first presence of a specific element is to be removed from deque, incase there are more than one such elements otherwise, it removes that particular element. The deque remains unchanged if the specified element does not exist in it and returns false otherwise, returns true. 

Parameter description

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

Example of removeFirstOccurrence(Object o) method

In this example we will show you how does removeFirstOccurrence(Object o) method work in Deque interface. Through this example we will show how you can delete the first 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 DequeRemoveFirstOccurrence {
  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.removeFirstOccurrence(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, 4, 3, 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