1. Java Stack class
In Java Stack class is a child class of Vector class in collection framework.
A
Stack
class represents a last-in-first-out (LIFO) stack of objects. AStack
supports two principle operations calledpush
andpop
.
2. Java Stack data structure :
The Stack
class data structure is Last-In-First-Out (LIFO). Below Image illustrates java Stack data structure.
3. When do we use Stack in Java?
A Stack
is ideal fit to use whenever last in first out (LIFO) order required. But java documentation recommend for more complete and consistent set of LIFO stack operations is provided by the Deque
interface and its implementations like ArrayDeque
, which should be used in preference to this class. A Deque
is double ended queue, it allows both LIFO and FIFO.
All the Stack
methods are Synchronized, if Synchronization needed and LIFO order required you can go for Stack
, otherwise it’s advised to use ArrayDeque
.
4. Class Hierarchy of Java Stack :
5. Stack class methods and examples
Following are the Stack class methods, However as Stack
extends Vector
class, by inheritance we can use Collection
, List
, Vector
methods also using stack instance.
Object push(Object element
) : push() method use to push an item onto the top of the stack.Object pop()
: pop() method use to remove the object at the top of the stack and returns that object as the value.Object peek()
: peek() method use to looks at the object at the top of this stack without removing it from the stack.boolean empty()
: It returns true if nothing is on the top of the stack. Else, returns false.int search(Object element)
: search() method returns position of the element if element is exist, otherwise returns -1. This method can used to determine an element in the stack exist or not.
5.1. Add elements to Stack example
public class JavaStackAddEelemntsDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); // adding elements to Stack, using stack push() method stack.push("Peter"); stack.push("Martin"); // adding elements to Stack, using List and Vector, Collection methods stack.add("Alex"); stack.add(3, "Vijay"); stack.addElement("Mike"); System.out.println("Stack => " + stack); } }
Output :
Stack => [Peter, Martin, Alex, Vijay, Mike]
5.2. Remove elements from Stack example
public class RemoveElementsFromStackDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); stack.push("Peter"); stack.push("Martin"); stack.push("Mike"); stack.push("John"); stack.push("Anand"); stack.push("Alex"); stack.push("Natarajan"); stack.push("Gerhard"); stack.push("Stefen"); //Stack.pop() - last-in-first out, and removes top System.out.println("Stack.pop()1 => " + stack.pop()); System.out.println("Stack.pop()2 => " + stack.pop()); stack.remove(1);// by index stack.removeElement("Mike"); stack.removeIf(name -> name.contains("G")); System.out.println(stack); } }
Output :
Stack.pop()1 => Stefen Stack.pop()2 => Gerhard [Peter, John, Anand, Alex, Natarajan]
5.3. Retrieve elements from Stack example
public class GetElementsFromStackDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); //Adding elements to Stack stack.push("Peter"); stack.push("Martin"); stack.push("Mike"); stack.push("John"); stack.push("Anand"); //Stack.peek() - last-in-first out System.out.println("Stack.peek()1 => " + stack.peek()); System.out.println("Stack.peek()2 => " + stack.peek()); System.out.println(stack.get(1));// by index System.out.println(stack.firstElement());// by Vector method System.out.println(stack.lastElement());// by Vector method } }
Output :
Stack.peek()1 => Anand Stack.peek()2 => Anand Martin Peter Anand
5.4. Search in Stack elements example
public class JavaStackSearchElementsDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); stack.push("Peter"); stack.push("Martin"); stack.push("Mike"); stack.push("John"); stack.push("Anand"); //Stack.search() - returns position if exist, else -1 System.out.println("Stack.search(Mike) => " + stack.search("Mike")); System.out.println("Stack.search(Anand) => " + stack.search("Anand")); System.out.println("Stack.search(Vijay) => " + stack.search("Vijay")); } }
Output :
Stack.search(Mike) => 3 Stack.search(Anand) => 1 Stack.search(Vijay) => -1
5.5. Iterate or loop Stack example
public class JavaStackIterateDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); //Adding elements to Stack stack.push("Peter"); stack.push("Martin"); stack.push("Mike"); stack.push("John"); stack.push("Anand"); System.out.println("------- using Enumeration --------"); Enumeration<String> e = stack.elements(); while(e.hasMoreElements()) { System.out.println("Person => "+e.nextElement()); } System.out.println("------- using Stream.forEach() --------"); stack.stream().forEach(p -> System.out.println("Person => "+p)); System.out.println("------- using enhanced for-each loop --------"); for(String s : stack) System.out.println("Person => "+s); } }
Output :
------- using Enumeration -------- Person => Peter Person => Martin Person => Mike Person => John Person => Anand ------- using Stream.forEach() -------- Person => Peter Person => Martin Person => Mike Person => John Person => Anand ------- using enhanced for-each loop -------- Person => Peter Person => Martin Person => Mike Person => John Person => Anand
5.6. Filter Stack elements using Stream example
public class FilteringStackElementsDemo { public static void main(String[] args) { Stack<String> stack = new Stack<>(); //Adding elements to Stack stack.push("Peter"); stack.push("Martin"); stack.push("Mike"); stack.push("John"); stack.push("Anand"); stack.stream(). filter(name -> name.startsWith("M")). forEach(p -> System.out.println("Person => "+p)); } }
Output :
Person => Martin Person => Mike