Javatpoint Logo
Javatpoint Logo

Java Pop

Java programming supports different data structures like array, linked list, stack, queue, etc. Each data structure has operations such as insertion, deletion, searching an element. And to implement these operations Java programming provides built in classes and methods. In this section, we will understand the pop operation with the help of Stack.pop() method.

Java Pop

Java provides three pop() methods that belong to three different classes and interfaces are as follows.

  • Java Stack.pop()Method
  • Java LinkedList.pop()Method
  • Java Deque.pop()Method

Where Stack and LinkedList are classes and Deque is an interface. Let's see pop() method of the classes and interface.

Java Stack.pop() Method

The stack data structure in Java is a linear data structure and it is based on LIFO (Last In First Out) approach. The Stack class is defined in Java Collection framework that belongs to java.util package. A stack can be implemented using two ways.

  • Using an array
  • Using Java Collections framework

The stack data structure implements different operations. Such as push that means adding a new element, pop that means deleting an existing element, search means finding the specified element and check if the stack is empty.

Syntax:

It returns the object at the top of the stack i.e., the last item of the Vector object. It throws EmptyStackException if the stack is empty.

The following program shows the implementation of pop operation using the Stack class.

StackPopDemo.java

Output:

Contents of Stack after addition of elements: [My, First, Stack, Implementation]
Popped element 1: Implementation
Popped element 2: Stack
Contents of Stack after deletion of elements: [My, First]

In the above code, the stack data structure is implemented by creating an instance stk of the Stack class. Basic operations are performed using built-in methods push() and pop().

Java LinkedList.pop() Method

The Java LinkedList class implements a doubly linked list to store the elements. The Java collections provides the definition of LinkedList class inside the java.util package. The pop() method of the class, removes the element at top of the stack.

Syntax:

The method is specified in interface Deque<E>. It returns the element at the front of the list i.e. top of the stack represented by this list. >. It works the same as removeFirst() method.

It throws NoSuchElementException if the list is empty.

The following program shows the implementation of pop operation in a LinkedList represented using a stack.

LinkedListPopDemo.java

Output:

Linked List after adding new elements: [Implementating, Linked, List]
Deleted element 1: Implementating
New Linked List: [Stack, Linked, List]

In the above code, a linked list is implemented using stack representation. And the push() and pop() methods are used to add and delete elements of the linked list.

Java Deque.pop() Method

The deque (double-ended queue) is a linear data structure that allows insertion and deletion from both the ends. The java.util package provides an interface Deque. It is implemented by various classes such as ArrayDeque, LinkedList, etc.

Syntax:

It returns the element at the front of the deque i.e., the top of the stack represented by this deque. It throws NoSuchElementException if the deque is empty.

The following program shows the implementation of pop operation in a deque.

DequePopDemo.java

Output:

Deque after insertion: 
3
2
1
After deletion: 
2
1

In the above code, an instance dq of the Deque class is created. And the basic operations are implemented using push() and pop() methods.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA