A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Stack in Collection in java

import java.util.Stack;
public class stackexample {
    public static void main(String agrs[])
            {
                Stack b=new Stack();
                b.push(10);
                b.push(20);
                b.push(30);
                b.push("Ritu");
               
                System.out.println("elements are "+ b);
               System.out.println("top element is "+  b.peek());
               b.pop();
              
                b.pop();
               System.out.println("elements are "+ b);
               System.out.println("top element is "+  b.peek());
            }
   
}


output:


Let's see another Example:


import java.util.Stack;
/**
 *
 * @author heerababu
 */
public class Stack_example {
    public static void main(String args[])
    {
        Stack s1= new Stack();
        s1.push(10);
        s1.push(12);
        s1.push(11);
        System.out.println(s1.peek());//11
        
        System.out.println(s1);//[10, 12, 11]
        
        s1.pop();
        System.out.println(s1);//[10, 12]
        
    }
}

output:



LEARN TUTORIALS

.

.