Home » Java programming language

Stack class in Collection Framework in Java

In this tutorial, we are going to learn what is Stack class in Collection Framework in Java? What is the use of Stack class in Collection Framework in Java?
Submitted by Preeti Jain, on August 04, 2019

Stack Class

  • Stack class is available in java.util package.
  • The Stack Class is child class of Vector class.
  • As we know that Stack is LIFO (Last-In-First-Out) data structure.
  • Here the above point given LIFO means last inserted element will be popped first.
  • Stack class contains only one constructor i.e. Default Constructor.
  • The syntax of default constructor is given below:
    Stack st = new Stack();
    
  • Stack class defines various methods to perform various task onto the stack. The names of the stack class methods is given below:
    1. void push(Object obj)
    2. Object pop()
    3. Object peek()
    4. boolean empty()
    5. int search(Object obj)
  • Now, we will see the purpose of the Stack methods given above and we will study one by one.

i) void push(Object obj)

  • This method is used to push an element into the stack.
  • This method takes one parameter in the method of the Stack and the parameter is the object is to be inserted into the Stack.

ii) Object pop()

  • This method is used to remove an element from the Stack and then it returns to the top of the Stack.
  • This method does not pass any parameter in the method of the Stack.

iii) boolean empty()

  • This method is used to check whether the Stack is empty or not.
  • We don't pass any object as a parameter in the method of the Stack.
  • The return type of this method is Boolean so it returns true if Stack is empty.

iv) Object peek()

  • This method is used to return top of the Stack.
  • We don't pass any object as a parameter in the method of the Stack.

v) int search(Object obj)

  • This method is used to search an element into the stack.
  • We pass only one object as a parameter in the method of the Stack and the parameter is the object is to be searched into the Stack.
  • The return type of this method is int so if an object is found in Stack so it returns offset or address from the top of the Stack else it returns -1 if an object is not found onto the Stack.

Example:

// Java program to demonstrate the behavior of the Stack.
import java.util.*;

class StackClass {
    public static void main(String[] args) {
        // Creating an instance of Stack
        Stack st = new Stack();

        // By using push() method to push few elements onto the Stack
        st.push(10);
        st.push(20);
        st.push(30);
        st.push(40);
        st.push(50);

        // Display Current Stack
        System.out.println("Current Stack List:" + st);

        // By using pop() method to remove an element from the 
        // stack and return top of the Stack
        System.out.println("The Popped Element:" + st.pop());

        // By using peek() method to return top of the Stack
        System.out.println("The top of the Stack:" + st.peek());

        // By using search() method to search an element from the Stack
        System.out.println("Search element is:" + st.search(30));
        System.out.println("Search element not found:" + st.search(60));
    }
}

Output

E:\Programs>javac StackClass.java

E:\Programs>java StackClass
Current Stack List:[10, 20, 30, 40, 50]
The Popped Element:50
The top of the Stack:40
Search element is:2
Search element not found:-1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.