stack collection java code example
Example 1: java stack
Stack<String> stack = new Stack<String>();
stack.push("Hello");
String top = stack.pop();
String peek = stack.peek();
Example 2: stack class in java
import java.util.Stack;
class Main {
public static void main(String[] args) {
Stack<String> animals= new Stack<>();
animals.push("Dog");
animals.push("Horse");
animals.pop();
animals.peek();
}
}
Example 3: java stack pop
package com.tutorialspoint;
import java.util.*;
public class StackDemo {
public static void main(String args[]) {
Stack st = new Stack();
st.push("Java");
st.push("Source");
st.push("code");
System.out.println("Removed object is: "+st.pop());
System.out.println("Elements after remove: "+st);
}
}