Java Program to demonstrate stack class in Collection framework

Program

package stackPack;
import java.util.*;
public class StackCollectionDemo
{
	public static void main(String[] args)
	{
		Stack st = new Stack();
		System.out.println("Contents of stack:\t" + st);
		System.out.println("Is stack empty?:\t" + st.empty());
		st.push(15);
		st.push(25);
		st.push(35);
		st.push(45);
		System.out.println("Contents of stack:\t" + st);
		System.out.println("Index of searched element (15):\t" + st.search(15));
		System.out.println("Top of the stack is:\t" + st.peek());
		st.pop();
		st.pop();
		System.out.println("Contents after pop operation:\t" + st);
	}
}

Output

$ javac -d . StackCollectionDemo.java 
$ java stackPack.StackCollectionDemo 
Contents of stack:	[]
Is stack empty?:	true
Contents of stack:	[15, 25, 35, 45]
Index of searched element (15):	4
Top of the stack is:	45
Contents after pop operation:	[15, 25]

Tags: