Facebook Page Twitter Page LinkedIn Page
× Java Utility Library


Java Stack Class

Java.util package provides a stack class which models and implements stack data structure. A stack is a linear dynamic data structure that follows Last-In/First-Out (LIFO) principle. In a stack, addition of a new element and deletion of an element occurs at the same end which implies that the element which is added last in the stack will be the first to be removed from the stack.

Features of stack

  • It is a dynamic data structure.
  • It has dynamic size.
  • It uses dynamic memory allocation.

Class declaration

The declaration of java.util.Stack class is:

public class Stack<E>
  extends Vector<E>

Here, E is the type of element maintained by the container.

Class Constructor

S.NConstructors & Description
1. Stack()
Creates an empty stack.

java.util.Stack Methods

The java.util.Stack class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean empty()
Returns true if the stack is empty, else returns false.
2. E peek()
Returns the top element of the stack (the last item of the stack).
3. E pop()
Deletes top element of the stack.
4. E push​(E element)
Adds a new element at the top of the stack.
5. int search(Object obj)
Returns the 1-based position where an object is on the stack.

Methods inherited from java.util.Vector

S.NMethods & Description
1. boolean add(E element)
Adds a new element at the top of the stack.
2. void add(int index, E element)
Inserts the specified element at the specified position in the stack.
3. boolean addAll(Collection<? extends E> c)
Appends all elements of the specified collection at the top of the stack.
4. boolean addAll(int index, Collection<? extends E> c)
Inserts all elements of the specified collection at the specified position in the stack.
5. void addElement​(E element)
Adds a new element at the top of the stack.
6. int capacity()
Returns current capacity of the stack.
7. void clear()
Clears all elements of the stack.
8. E elementAt(int index)
Returns element at the specified index in the stack.
9. E firstElement()
Returns the first element (bottom element) of the stack.
10. E get(int index)
Returns the element at specified index of the stack.
11. int indexOf(Object o)
Returns the index of the first occurrence of the specified element in the stack, or -1 if element is not present in the stack.
12. int indexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in the stack, searching forward from specified index, or -1 if element is not present in the stack.
13. boolean isEmpty()
Returns true if the stack is empty, else returns false.
14. E lastElement()
Returns the last element (top element) of the stack.
15. int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in the stack, or -1 if element is not present in the stack.
16. int lastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in the stack, searching backward from specified index, or -1 if element is not present in the stack.
17. E set(int index, E element)
Replaces the element at the specified index in the stack with the specified element.
18. void setElementAt(E obj, int index)
Sets the component at the specified index of the stack to be the specified object.
19. void setSize(int newSize)
Sets the size of the stack.
20. int size()
Returns the number of elements in the stack.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractList<E>
  • java.util.Vector<E>