Topics Index

Collections Framework > Legacy Classes and Interfaces >
Siva Nookala - 01 Apr 2016
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Java Vector with five more operations that allow a vector to be treated as a stack.

The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items.
Stack Constructor:
Stack()Creates an empty Stack.

Stack Methods:
boolean empty()Tests if this stack is empty.
Object peek()Looks at the object at the top of this stack without removing it from the stack.
Object pop()Removes the object at the top of this stack and returns that object as the value of this function.
Object push(Object element)Pushes an item onto the top of this stack.
int search(Object element)Returns the 1-based position where an object is on this stack.

import java.util.Stack;

class StackTest
    public static void main(String arg[])
        Stack<Integer> st = new Stack<Integer>();
        System.out.println("Is empty stack: " + st.empty());
        System.out.println("Stack A: " + st);
        System.out.println("Stack B:" + st);
        System.out.println("Stack C: " + st);
        System.out.println("Stack D: " + st);
        System.out.println("Stack E: " + st);
        System.out.println("Peek integer: " + st.peek());
        System.out.println("Position: " +;
        System.out.println("Stack F: " + st);
        System.out.println("Stack G: " + st);

Is empty stack: true
Stack A: []
Stack B: [42]
Stack C: [42, 64]
Stack D: [42]
Stack E: [42, 96]
Peek integer: 96
Position: 2
Stack F: [42]
Stack G: []


In this program, stack is created, checked for empty then adds two integers, pops top element and again adds one integer and then returns peek integer, position of 42 and pops twice.

  • Try for the below code.
    System.out.println("Stack Elements : " + st);
The output for the above code is Stack Elements : [1, 10, 100], since Stack class extends Vector class, methods of Vector class are applicable for Stack class as well.
  • Try for the below code :
    Stack<Integer> st = new Stack<Integer>();
    System.out.println("Stack Elements : " + st);
    The above code throws java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10, since here remove method accepts the argument as index value.
  • Try for the below code :
    Stack<String> st = new Stack<String>();
    System.out.println("Stack Elements : " + st);
    System.out.println("Stack Elements after removing Merit : " + st);
    The output for the above code is :
    Stack Elements : [Merit, Campus]
    Stack Elements after removing Merit : [Campus]

    Here the remove method accepts the argument as an object, so the element Merit is removed from the Stack.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App