tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collection > Stack > Basic Operations

Basic Operations 

The following example shows how to create Stack and its basic operations. Stack provides the following APIs,

public E push(E paramE)
 - Pushes an element onto the top of stack. 

public synchronized E pop()
 - Removes the element at top of stack.
 
public boolean empty()
 - Returns true if the stack is empty otherwise false.   


File Name  :  
com/bethecoder/tutorials/utils/stack/StackTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.stack;

import java.util.Stack;

public class StackTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Stack<String> stack = new Stack<String>();
    stack.push("ONE");
    stack.push("TWO");
    stack.push("THREE");
    stack.push("FOUR");
    System.out.println("Stack : " + stack);
    System.out.println();
    
    //Remove elements from stack
    //one by one until its empty
    while (!stack.isEmpty()) {
      System.out.println("Popped : " + stack.pop());
    }
  }

}
   

It gives the following output,
Stack : [ONE, TWO, THREE, FOUR]

Popped : FOUR
Popped : THREE
Popped : TWO
Popped : ONE



 
  


  
bl  br