Updated April 13, 2023
Introduction to Java Stack Methods
Java Stack Methods are part of the Stack class, which is used for prototyping and providing a model for the stack data structure. As part of the Stack class, Stack Methods behaves in last in first out fashion with addition to some common operations like push, pop, empty, search and peek, which is used for enhancing and giving flexibility to the operations. The stack method as part of the stack class extends the vector and accumulates all the five mentioned methods as one concrete stack class Thus, it can also be called a subclass of the vector.
Top Methods of Java Stack
Java Stack Methods plays a very important role in constructing the entire stack class as it involves the accumulation of all the five common operations :
1. Object push(Object Element)
This method is used for pushing an element on top of the stack.
Example: This program demonstrates the Object push method, which first represents a stack pushed with elements and then a stack that represents the stack after pushing two more elements on the stack, which gets added on the previous stack on top with other elements in Last In first-out fashion.
Code:
import java.util.Stack;
public class Stck_Psh_Ex {
public static void main(String[] args) {
Stack<String> Stk = new Stack<String>();
Stk.push("Educba");
Stk.push("is");
Stk.push("a");
Stk.push("learning");
Stk.push("portal");
System.out.println("Stack_Befr_Pushing_Elements: " + Stk);
Stk.push("Great");
Stk.push("Experience!");
Stk.push("for_Learning");
System.out.println("Stack_aftr_Pushing_Elements: " + Stk);
}
}
Output:
2. Object pop()
This method is part of the operation used to make the stack class used for removing and retrieving the top element of the stack. If the stack is empty and it does not contain any element on the stack, it will throw an exception whenever the stack invoked is empty.
Example: This program demonstrates the Object pop() method, which first represents a stack pushed with elements and then a stack which represents the stack after Popping out elements on the stack which is defined and is present in the previous stack after performing popping the final stack comes out to be elements after removing the popped out elements from the previous stack.
Code:
import java.util.Stack;
public class Stck_Pop_El {
public static void main(String[] args) {
Stack<String> Stck = new Stack<String>();
Stck.push("Hello");
Stck.push("Everyone");
Stck.push("Hope");
Stck.push("Things");
Stck.push("Are");
Stck.push("Ok");
System.out.println("Stack_Before_Performing_Pop: " + Stck);
System.out.println("Popping of Elements: " + Stck.pop());
System.out.println("Popping of Elements: " + Stck.pop());
System.out.println("Stack_after_Performing_Pop "+ Stck);
}
}
Output:
3. boolean Empty()
This method is part of the stack that will be used for returning a value as true if the stack is returning a value that is not present and returns false if the value is present on top of the stack.
Example: This program is used to demonstrate the boolean Empty() method of the Java Stack Method where a stack is defined initially, and then the stack is performed with the popping activity to make and represent the stack as empty, and it signifies whether the stack is satisfying the condition for empty or not using by throwing the true or false value based on the condition.
Code:
import java.util.Stack;
public class Stk_Empty_Ex {
public static void main(String[] args) {
Stack<String> Stck = new Stack<String>();
Stck.push("The");
Stck.push("World");
Stck.push("is");
Stck.push("a");
Stck.push("Happy");
Stck.push("Place");
System.out.println("Current Stack " + Stck);
System.out.println("Elements_Empty_In_Stack " + Stck.empty());
Stck.pop();
Stck.pop();
Stck.pop();
Stck.pop();
Stck.pop();
Stck.pop();
System.out.println("Stack is now Empty. " +
Stck.empty());
}
}
Output:
4. Object peek()
This method is part of the operation used for constructing the stack class and is used to return an element that is present at the top of the stack but is not used to remove the element at the top of the stack.
Example: This program is used for demonstrating the Object peek() method, which initially defines a stack with pushing the elements and then it performs a peek operation on the initial stack, which represents and points out the top of the stack and helps in retrieving the element from the top of the stack to display for modification.
Code:
import java.util.Stack;
public class Stck_Peek {
public static void main(String[] args) {
Stack<String> Stk1 = new Stack<String>();
Stk1.push("Some");
Stk1.push("Ray");
Stk1.push("Of");
Stk1.push("Hope");
Stk1.push("Needed");
System.out.println("Stack_Before_Performing_Peek: " + Stk1);
System.out.println("Peeking_Top_Stack:" + Stk1.peek());
System.out.println("Stack_After_Performing_Peek: " + Stk1);
}
}
Output:
5. int search(Object Element)
This method is part of the stack method’s common operation for searching the elements and checking whether an object is present within the stack. If the element is searched and matches the element exactly with the element present at the top of the stack, it returns the value; otherwise, it will return the value as -1.
Example: This program is used for demonstrating the int search(Object element), which is used for searching the element from the stack where it is initialized and defined for the initial stack. If the initial stack consists of the element, it will return the value as if, if not able to search the element, it will return the value with a negative value.
Code:
import java.util.Stack;
public class St_Search {
public static void main(String[] args) {
Stack<String> St = new Stack<String>();
St.push("Exercise");
St.push("is");
St.push("Good");
St.push("For");
St.push("Health");
System.out.println("Present_Stack: " + St);
System.out.println("Stack searching for the element: "
+ St.search("all"));
System.out.println("Stack searching for the element: "
+ St.search("Good"));
System.out.println("Stack searching for the element: "
+ St.search("For"));
}
}
Output:
Conclusion
Java Stack Methods are very useful for constructing the stack class, which gives an aid to the entire Java collection Framework by making it as flexible and versatile as it can make use of the subclass such as vector class which in turn makes use of all the operations like pop, push, peek, empty and search. It helps by providing a prototype for the stack data structure.
Recommended Articles
This is a guide to Java Stack Methods. Here we discuss Java Stack Methods’ concept through definition and their methods along with programming examples and their outputs. You can also go through our other suggested articles to learn more –