Java Stack Example explains you how the stack feature can be implemented into the world of Java programming.
Java Stack Example
In this section we will read about how you can provide the feature of bounded capacity in your application.
Stack is a particular collection which is an implementation of LIFO (Last In First Out) data structure. As this data structure is named, the elements which are added to the structure in last must be removed from the structure at first. The principal operation of Stack is to insertion of entities, called push operation and removal of entities, called pop operation. In stack, the push and pop operation is performed at only one side of the structure i.e. top of the stack. The implementation of stack may be considered as the Linear Data Structure, or the Sequential Collection. Stack may have the two states while pushing and popping is occurred. These are Overflow and Underflow.
- Overflow : Overflow is the state in which the stack is reached when the push operation is performed on the stack and it has no more space to be pushed in.
- Underflow : Underflow is the state in which the stack is reached when the pop operation is performed on the stack and there is no more element is to be removed i.e. the stack is empty.
In Java Stack may be implemented using the java.util.Stack class. This class provides methods for performing the operations on Stack like push, pop, peek(peek is the operation in which the top element is retrieved without removing it from stack), check for the empty stack, search for an item in the stack and its position from the top element.
Constructor Detail
This class has one constructor.
Stack() : This constructor is used to create an object of empty stack.
Method Detail
This class provides methods for performing various operations on stack. These are as follows :
- boolean empty() : This method is used to check the
availability of element in the stack. It returns true if the stack is empty
otherwise returns false.
Syntax : public boolean empty()
- E peek() : This method is used to look the element at the
top of the stack. It returns the top element but not removes that element. This
method may throw an EmptyStackException if the stack has no element to look up.
Syntax : public E peek()
- E pop() : This method returns the top element of the stack
as well as removing it from the stack. Generally, this method is used to delete
the top element of the stack. This method may throw an EmptyStackException, if
the stack has no elements to remove.
Syntax : public E pop()
- E push(E item) : This method is used to push the element
into the stack. The newly pushed element is always reside on the top of the
stack.
Syntax : public E push(E item)
- int search(Object o) : This method is used to search an
item into the stack with its position from the top item of the stack. The
position of the top most item in the stack is to be returned as 1. It returns
the position of item in the stack as positive integer value and the value -1
shows there is not item available in the stack.
Syntax : public int search(Object o)
Example
Here an example is being given which will demonstrate you about how to use the stack collection in the Java programming. In this example you will see how the item can be pushed, popped, searched, peek, etc. For this I will create a simple Java class where I will import the necessary package java.util.Stack and then I will create an object of stack and then I will performed the above said operations on stack.
StackExample.java
import java.util.Stack; public class StackExample { public static void main(String args[]) { Stack stack = new Stack(); stack.push("Java");//pushed first into the stack so it will be the last item in stack stack.push("Stack"); stack.push("Example");//pushed last into the stack so it will be the first item in stack //check whether the stack is empty or not System.out.println("Is stack empty : "+stack.isEmpty()); //Retrieve the top item of the stack without removing it from the stack System.out.println("Top item of Stack : "+stack.peek()); //Search for the items position in the stack //Return 1 because 'Example' is the top item of stack System.out.println("Position of item 'Example' in the stack is : "+stack.search("Example")); // remove top item of the stack stack.pop();//remove 'Example' from the stack //check whether the top element is removed or not //Returns -1 because the item 'Example' is not available in the stack System.out.println("Position of item 'Example' in the stack : " +stack.search("Example")); } }
Output
When you will compile and execute the above Java class then the output will be as follows :