Stack in Java
The Stack is the child class of Vector. The Stack class extends the Vector class with five operations. These operations allow a vector to be treated as a Stack. Stack represents a LIFO (Last In First Out) order of objects. It usually provides the push and pop operations as well as a method to peek at the last item on the Stack. It provides a method to test whether the stack is empty or not and a method to search an element in the Stack to discover how far it is from the top.
When a Stack is initialized, it is empty or contains no items.
Syntax to create a Stack:
Stack s= new Stack();
This constructor creates an empty Stack with the name s.
Methods of Stack and its usages:
Modifier and Data Type | Methods | Description |
Boolean | empty() | The method tests if the stack is empty or not. |
Object E | push(E item) | This method pushes (insert) an item onto the top of the stack. |
pop() | It removes the element from the top of the stack and returns that element as the value of this method. | |
peek() | This method returns the element from the top of the stack without removing it from the stack. | |
int | Search(Object o) | This method determines if an element exists in the stack. If the element is present in the stack, then it returns the index position of that element from the top of the stack. Otherwise, it returns -1. |
Example to illustrate the push() method.
import java.util.*; class StackJava { public static void main(String args[]) { // Creating an empty Stack Stack s = new Stack(); // Using push() method to add elements into the Stack s.push("Welcome"); s.push("To"); s.push("Tutorials"); s.push("And"); s.push("Examples"); // printing the initial stack System.out.println("Stack: " + s); // Pushing additional values into the stack s.push("Java"); s.push("Stack"); // Displaying the final Stack after adding new elements System.out.println("Final Stack: " + s); } }
Output:
Stack: [Welcome, To, Tutorials, And, Examples] Final Stack: [Welcome, To, Tutorials, And, Examples, Java, Stack]
Example to illustrate pop() method.
import java.util.*; class StackJava { public static void main(String args[]) { // Creating an empty Stack Stack s = new Stack(); // Using push() method to add elements into the Stack s.push("Welcome"); s.push("To"); s.push("Tutorials"); s.push("And"); s.push("Examples"); // printing the initial stack with all the elements System.out.println("Stack: " + s); // Removing elements using pop() method System.out.println("Popped element: " + s.pop()); System.out.println("Popped element: " + s.pop()); // Displaying the final Stack after popping the elements System.out.println("Final Stack after removing the elements: " + s); } }
Output:
Stack: [Welcome, To, Tutorials, And, Examples] Popped element: Examples Popped element: And Final Stack after removing the elements: [Welcome, To, Tutorials]
Example to illustrate the peek() method.
import java.util.*; class StackJava { public static void main(String args[]) { // Creating an empty Stack Stack s = new Stack(); // Using push() method to add elements into the Stack s.push("Welcome"); s.push("To"); s.push("Tutorials"); s.push("And"); s.push("Examples"); // printing the initial stack with all the elements System.out.println("Stack: " + s); // Fetching the element from the head of the Stack System.out.println("The object at the head of the stack is: " + s.peek()); // Displaying the final Stack System.out.println("Final Stack after the peek operation: " + s); } }
Output:
Stack: [Welcome, To, Tutorials, And, Examples] The object at the head of the stack is: Examples Final Stack after the peek operation: [Welcome, To, Tutorials, And, Examples]
Example to illustrate empty() method.
import java.util.*; class StackJava { public static void main(String args[]) { // Creating an empty Stack Stack s = new Stack(); // Using push() method to add elements into the Stack s.push("Welcome"); s.push("To"); s.push("Tutorials"); s.push("And"); s.push("Examples"); // printing the initial stack with all the elements System.out.println("Stack: " + s); // Checking for the emptiness of stack System.out.println("Stack is empty: " + s.empty()); //removing all the elements System.out.println("Popped element: " + s.pop()); System.out.println("Popped element: " + s.pop()); System.out.println("Popped element: " + s.pop()); System.out.println("Popped element: " + s.pop()); System.out.println("Popped element: " + s.pop()); // Checking for the emptiness of stack System.out.println("Stack is Empty: " + s.empty()); } }
Output:
Stack: [Welcome, To, Tutorials, And, Examples] Stack is empty: false Popped element: Examples Popped element: And Popped element: Tutorials Popped element: To Popped element: Welcome Stack is Empty: true
Example to illustrate search() method.
import java.util.*; class StackJava { public static void main(String args[]) { // Creating an empty Stack Stack s = new Stack(); // Using push() method to add elements into the Stack s.push("Welcome"); s.push("To"); s.push("Tutorials"); s.push("And"); s.push("Examples"); // printing the initial stack with all the elements System.out.println("Stack: " + s); // Checking for the element "Tutorials" System.out.println("Do the stack contains 'Tutorials': " + s.search("Tutorials")); // Checking for the element "Ankit" System.out.println("Does the stack contains 'Ankit'? "+ s.search("Geeks")); } }
Output:
Stack: [Welcome, To, Tutorials, And, Examples] Do the stack contains 'Tutorials': 3 Does the stack contains 'Ankit'? -1