📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
What is a Stack?
Stack Concepts
- When an element is inserted in a stack, the concept is called a push.
- When an element is removed from the stack, the concept is called pop.
- Trying to pop out an empty stack is called underflow (treat as Exception).
- Trying to push an element in a full stack is called overflow (treat as Exception).
Stack Class Methods Summary
- boolean empty() - This method is used to test if this stack is empty.
- E peek() - This method looks at the object at the top of this stack without removing it from the stack.
- E pop() - This method is used to remove the object at the top of this stack and returns that object as the value of this function.
- E push(E item) - This method pushes an item onto the top of this stack.
- int search(Object o) - This method returns the 1-based position where an object is on this stack.
Stack Class Diagram
Stack Class Methods with Example
push(String item)
private static void pushMethod() {
// creating stack
Stack < String > stack = new Stack < > ();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
// checking elements
System.out.println("Elements in the stack: " + stack);
}
Elements in the stack: [Java, JEE, C, C++, Spring, Hibernate]
E pop()
private static void popMethod() {
// creating stack
Stack < String > stack = new Stack < > ();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
// removing top object
System.out.println("Removed object is: " + stack.pop());
// elements after remove
System.out.println("Elements after remove: " + stack);
}
Removed object is: Hibernate
Elements after remove: [Java, JEE, C, C++, Spring]
int search(Object o)
private static void searchMethod() {
// creating stack
Stack < String > stack = new Stack < > ();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
// searching 'Spring' element
System.out.println("Searching 'Spring' in stack: " + stack.search("Spring"));
}
Searching 'Spring' in stack: 2
E peek()
private static void peekMethod() {
// creating stack
Stack < String > stack = new Stack < > ();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
// checking the top object
System.out.println("Top object is: " + stack.peek());
}
Top object is: Hibernate
boolean empty()
private static void emptyMethod() {
// creating stack
Stack < String > stack = new Stack < > ();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
// checking stack
System.out.println("Is stack empty: " + stack.empty());
}
Is stack empty: false
Java Array to Stack Example
package com.javaguides.corejava.api.util;
import java.util.Stack;
/**
* Class demonstrates the usage of Stack class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class ArrayToStackExample {
public static void main(String[] args) {
convertArrayToStack();
}
private static void convertArrayToStack() {
String[] strArr = {
"Java",
"JEE",
"C",
"C++",
"Spring",
"Hibernate"
};
Stack < String > stack = new Stack < > ();
for (String string: strArr) {
stack.push(string);
}
System.out.println("Non-Empty stack : " + stack);
}
}
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]
Java List to Stack Example
package com.javaguides.corejava.api.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Class demonstrates the usage of Stack class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class ListToStackExample {
public static void main(String[] args) {
convertListToStack();
}
private static void convertListToStack() {
Stack<String> stack = new Stack<>();
List<String> list = new ArrayList<>();
list.add("Java");
list.add("JEE");
list.add("C");
list.add("C++");
list.add("Spring");
list.add("Hibernate");
System.out.println("Non-Empty stack addAll Operation : " + stack.addAll(list));
System.out.println("Non-Empty stack : " + stack);
}
}
Non-Empty stack addAll Operation : true
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]
Java Stack to List Example
package com.javaguides.corejava.api.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Class demonstrates the usage of Stack class methods with examples
*
* @author Ramesh Fadatare
*
*/
public class StackToListExample {
public static void main(String[] args) {
convertStackToList();
}
private static void convertStackToList() {
// creating stack
Stack<String> stack = new Stack<>();
// populating stack
stack.push("Java");
stack.push("JEE");
stack.push("C");
stack.push("C++");
stack.push("Spring");
stack.push("Hibernate");
List<String> list = new ArrayList<>();
list.addAll(stack);
System.out.println("Non-Empty stack : " + stack);
System.out.println("Non-Empty List : " + list);
}
}
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]
Non-Empty List : [Java, JEE, C, C++, Spring, Hibernate]
Learn complete core Java at Java Tutorial | Learn Java Programming with Examples
Comments
Post a Comment
Leave Comment