pop() method of Deque Interface in Java.

pop() method of Deque Interface in Java.


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

E pop() This method pops the head position element from the stack represented by the underlying deque.

pop() method of Deque Interface in Java.

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

Syntax

E pop()

This method pops the head position element from the stack represented by the underlying deque.

In this method the element at the head position is returned and removed from the deque. It displays 'NoSuchElementException' if the deque is empty otherwise, it returns the front element which has been just retrieved and removed from the deque. This method is equivalent to the removeFirst() method.

Parameter description

This method has no parameter.

Example of pop() method

In this example we will show how does pop() method work in Deque interface. This example will help you to understand how you can pop the front element, that is remove the first element from the deque and what the exception it displays when deque is empty.

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Iterator;
public class DequePop {
  public static void main(String[] args) {
    Deque<Integer> dq = new LinkedList<Integer>();
    dq.add(-1);
    dq.add(0);
    dq.add(1);
    dq.add(2);
    dq.add(3);
    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 element from the deque new deque is ");
    Object obj = dq.pop();
    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.pop();
    }
    System.out.println("Elements in deque = "+dq);
    System.out.println("Is deque empty : " + dq.isEmpty());
    // Here implementation of the pop( ) method will display 'NoSuchElementException'
    System.out.println(dq.pop());
  }
}

Output :

Is deque empty : false

Elements of deque : [-1, 0, 1, 2, 3]

Size of deque before removing an element : 5

Now after removing the element from the deque new deque is

[0, 1, 2, 3]

Removed element is = -1

Size of deque after removing element : 4

Elements in 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 java.util.LinkedList.pop(Unknown Source)

at devmanuals.com.DequePop.main(DequePop.java:35)

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics