removeFirst() method of Deque Interface in Java.

removeFirst() method of Deque Interface in Java.


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

E removeFirst() This method removes the element from the head of the deque.

removeFirst() method of Deque Interface in Java.

In the following example we will show how can removeFirst() method be implemented in Deque Interface in java.

Syntax

E removeFirst()

This method removes the element from the head of the deque.

In this method the head element of deque is deleted. This method is equivalent to the pollFirst() method as this method retrieves and deletes the element at the head position of the deque, the removeFirst() method also works similar to the pollFirst() method. This method is slightly different from the pollFirst() method in terms the removeFirst() method and displays 'NoSuchElementException' whereas pollFirst() method returns 'null' if the underlying deque is empty. 

Parameter description

This method does not take any argument, but it retrieves and deletes the first element of the underlying deque, or throws exception if deque is empty.

Example of removeFirst() method 

In this example we will show you how does removeFirst() method work in Deque interface. This example will help you to understand how can you delete the first element of the underlying deque and count the total number of elements in deque.

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Iterator;
public class DequeRemoveFirst {
  public static void main(String[] args) {
    Deque dq = new LinkedList();    
    dq.add(10);
    dq.add(11);
    dq.add("Java");
    dq.add("Dev");
    dq.add("Manuals");
    dq.add(".Com");
    System.out.println("Is deque empty : " + dq.isEmpty());
    System.out.println("Elements of deque : " + dq);
    System.out.println("Size of deque before removing an element : "+dq.size());
    System.out.println("Now after removing the first element from deque new deque is ");
    Object obj = dq.removeFirst();
    System.out.println(dq);
    System.out.println("Removed element is = " + obj);
    System.out.println("Size of deque after removing element : "+ dq.size());
    //Removes all the elements from deque
    Iterator it = dq.iterator();
    while (it.hasNext()) {
      dq.remove();
    }
    System.out.println("After removing all the elements ");
    System.out.println("from deque then deque = "+dq);
    System.out.println("Is deque empty : " + dq.isEmpty());
    // Here implementation of the removeFirst( ) method will display an exception
    System.out.println(dq.removeFirst());
  }
}

Output :

Is deque empty : false

Elements of deque : [10, 11, Java, Dev, Manuals, .Com]

Size of deque before removing an element : 6

Now after removing the first element from deque new deque is

[11, Java, Dev, Manuals, .Com]

Removed element is = 10

Size of deque after removing element : 5

After removing all the elements

from deque then deque = []

Is deque empty : true

Exception in thread "main" java.util.NoSuchElementException

at java.util.LinkedList.remove(Unknown Source)

at java.util.LinkedList.removeFirst(Unknown Source)

at devmanuals.com.DequeRemoveFirst.main(DequeRemoveFirst.java:37)

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics