Site icon TestingDocs.com

Java Stack Class

Overview

In this tutorial, we will learn about the in-built Java Stack Class. Java Stack class is defined in java.util package.

Java Stack class

It represents a stack of objects in a LIFO manner (Last-In-First-Out). The Stack class extends the class Vector.

public class Stack<E> extends Vector<E> {

}

E specifies the type of element stored in the stack.

We can import the class using the following statement:

import java.util.Stack;

Methods

Stack inherits all the methods defined by the Vector class and adds several methods on its own. The class supports five operations. The operations are as follows:

 

Stack method
Description
 

push(E)

This method pushes an item onto the top of the stack. We can call the push() method, to put an object onto the top of the stack.
 

E pop()

This method returns the top of the stack and removes the object at the top of the stack. To remove and return the top element, call the pop() method.
E peek()

 

This method returns the object at the top of the stack. Unlike the pop() method it does not remove it from the stack. We can use the peek() method to return, but not remove the top of the stack object.
boolean empty()

 

This method tests if the stack is empty or not. This method returns true if the stack is empty, and returns false if the stack
contains elements.
int search(Object)

 

This method searches for the element in the stack. If found it returns the distance from the top of the stack (offset) to the occurrence nearest the top of the stack. Otherwise, the method returns -1.

 

We can create the stack using the following statement. For example, to create a stack to hold Integer objects.

Stack<Integer> stack = new Stack<Integer>();

It creates an empty stack. When a stack is created, it contains no items.

Example

 

package com.testingdocs.java.tutorials;

import java.util.Stack;

/*
 * Java Stack class demo
 * Java Tutorials - www.TestingDocs.com
 */
public class StackDemo {

	public static void main(String[] args) {
		Stack<Integer> stack = new Stack<Integer>();
		//Let's push three numbers
		stack.push(10);
		stack.push(20);
		stack.push(30);
		System.out.println("Stack:="+ stack); 
		System.out.println("Top of stack=" + stack.peek()); 
		System.out.println("Check if stack is empty=" + stack.empty()); 
		System.out.println("Search for 30=" + stack.search(30)); 
		// Invoke pop() method
		stack.pop();
		System.out.println("Stack:="+ stack);
		System.out.println("Top of stack after pop=" + stack.peek()); 
		System.out.println("Search for 30 after pop=" + stack.search(30)); 
		stack.pop();
		stack.pop();
		System.out.println("Stack:="+ stack);
		System.out.println("Check if stack is empty=" + stack.empty()); 
	}

}


Sample Output

Stack:=[10, 20, 30]
Top of stack=30
Check if stack is empty=false
Search for 30=1
Stack:=[10, 20]
Top of stack after pop=20
Search for 30 after pop=-1
Stack:=[]
Check if stack is empty=true

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version