Stack
Java Collection framework provides a Stack class that models and implements a Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search, and peek.
Declaration
public class Stack<E> extends Vector<E>
All Implemented Interfaces
Serializable: It is a marker interface that classes must implement if they are to be serialized and deserialized.
Cloneable: This is an interface in Java which needs to be implemented by a class to allow its objects to be cloned.
Iterable
: This interface represents a collection of objects which is iterable — meaning which can be iterated. Collection
: A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired List
: The List interface provides a way to store the ordered collection. It is a child interface of Collection. RandomAccess: This is a marker interface used by List implementations to indicate that they support fast (generally constant time) random access.
Methods in Stack
METHODS | DESCRIPTION |
---|---|
empty() | It returns true if nothing is on the top of the stack. Else, returns false. |
peek() | Returns the element on the top of the stack, but does not remove it. |
pop() | Removes and returns the top element of the stack. An ‘EmptyStackException’. An exception is thrown if we call pop() when the invoking stack is empty. |
push(Object element) | Pushes an element on the top of the stack. |
search(Object element) | It determines whether an object exists in the stack. If the element is found. It returns the position of the element from the top of the stack. Else, it returns -1. |
add(Object obj) | Appends the specified element to the end of this Vector. |
add(int index, Object obj) | Inserts the specified element at the specified position in this Vector. |
addAll(Collection c) | Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection’s Iterator. |
addAll(int index, Collection c) | Inserts all the elements in the specified Collection into this Vector at the specified position. |
addElement(Object o) | Adds the specified component to the end of this vector, increasing its size by one. |
capacity() | Returns the current capacity of this vector. |
clear() | Removes all the elements from this Vector. |
Operations on Stack
Adding Elements
In order to add an element to the stack, we can use the push() method. This push() operation place the element at the top of the stack.
// Java program to add the
// elements in the stack
import java.io.*;
import java.util.*;
class StackDemo {
// Main Method
public static void main(String[] args)
{
// Default initialization of Stack
Stack stack1 = new Stack();
// Initialization of Stack
// using Generics
Stack<String> stack2 = new Stack<String>();
// pushing the elements
stack1.push(4);
stack1.push("Java");
stack1.push("Language");
stack2.push("Coding");
stack2.push("For");
stack2.push("All");
// Printing the Stack Elements
System.out.println(stack1);
System.out.println(stack2);
}
}
Output
[4, Java, Language]
[Coding, For, All]
Accessing the Element
To retrieve or fetch the first element of the Stack or the element present at the top of the Stack, we can use peek() method. The element retrieved does not get deleted or removed from the Stack.
// Java program to demonstrate the accessing
// of the elements from the stack
import java.util.*;
import java.io.*;
public class StackDemo {
// Main Method
public static void main(String args[])
{
// Creating an empty Stack
Stack<String> stack = new Stack<String>();
// Use push() to add elements into the Stack
stack.push("Welcome");
stack.push("To");
stack.push("Coding");
stack.push("For");
stack.push("All");
// Displaying the Stack
System.out.println("Initial Stack: " + stack);
// Fetching the element at the head of the Stack
System.out.println("The element at the top of the"
+ " stack is: " + stack.peek());
// Displaying the Stack after the Operation
System.out.println("Final Stack: " + stack);
}
}
Output
Initial Stack: [Welcome, To, Coding, For, All]
The element at the top of the stack is: All
Final Stack: [Welcome, To, Coding, For, All]
Removing Elements
To pop an element from the stack, we can use the pop() method. The element is popped from the top of the stack and is removed from the same.
// Java program to demonstrate the removing
// of the elements from the stack
import java.util.*;
import java.io.*;
public class StackDemo {
public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> stack = new Stack<Integer>();
// Use add() method to add elements
stack.push(10);
stack.push(15);
stack.push(30);
stack.push(20);
stack.push(5);
// Displaying the Stack
System.out.println("Initial Stack: " + stack);
// Removing elements using pop() method
System.out.println("Popped element: "
+ stack.pop());
System.out.println("Popped element: "
+ stack.pop());
// Displaying the Stack after pop operation
System.out.println("Stack after pop operation "
+ stack);
}
}
Output
Initial Stack: [10, 15, 30, 20, 5]
Popped element: 5
Popped element: 20
Stack after pop operation [10, 15, 30]
⌖
core
java
programming
stack
class
collections