Stack - Push and pop Employee object in java



In this Data structures tutorial we will learn how to push and pop Employee object in a stack in java with example and program. We will use generic implementation of stack to achieve it in java.


Full Program/Example to Push and pop Employee object in Stack implementation in java >
class Employee {
   private String id;
   private String name;
  
   /**
   * Employee constructor
   */
   public Employee(String id, String name) { // constructor
          this.id = id;
          this.name = name;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + ", name=" + name + "]   ";
   }
  
}
/**
*Exception to indicate that Stack is full.
*/
class StackFullException extends RuntimeException {
  
   public StackFullException(){
       super();
   }
   
   public StackFullException(String message){
       super(message);
   }
   
}
/**
*Exception to indicate that Stack is empty.
*/
class StackEmptyException extends RuntimeException {
  
   public StackEmptyException(){
       super();
   }
   
   public StackEmptyException(String message){
       super(message);
   }
   
}
/**
* Stack class(generic type)
*/
class Stack<T> {
   private int size;
   private T[] stackAr;
   private int top; // top of stack
   /**
   * Constructor for initializing Array.
   */
   @SuppressWarnings("unchecked")
   public Stack(int size) {
          this.size = size;
          stackAr = (T[])new Object[size]; //Creation of Generic Stack Array
          top = -1; // initialize Stack to with -1
   }
   /**
   * Push items in stack, it will put items on top of Stack.
   */
   public void push(T value){
          if(isFull()){
                 throw new StackFullException("Cannot push "+value+", Stack is full");
          }
          stackAr[++top] = value;
   }
   /**
   * Pop items in stack, it will remove items from top of Stack.
   */
   public T pop() {
          if(isEmpty()){
                 throw new StackEmptyException("Stack is empty");
          }
          return stackAr[top--]; // remove item and decrement top as well.
   }
   /**
   * @return true if Stack is empty
   */
   public boolean isEmpty(){
          return (top == -1);
   }
   /**
   * @return true if stack is full
   */
  
   public boolean isFull(){
          return (top == size - 1);
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* StackExampleGeneric Main class
*/
public class StackExampleGeneric {
   public static void main(String[] args) {
          Stack<Employee> stack = new Stack<Employee>(10); // Creation of Generic Stack
          stack.push(new Employee("11", "sam"));
          stack.push(new Employee("22", "sam"));
          stack.push(new Employee("33", "sam"));
          stack.push(new Employee("44", "sam"));
          stack.push(new Employee("11", "sam"));
         
          System.out.print("Popped items: ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
          System.out.print(stack.pop()+" ");
         
   }
}
/** OUTPUT
Popped items: Employee [id=11, name=sam] Employee [id=44, name=sam] Employee [id=33, name=sam] Employee [id=22, name=sam] Employee [id=11, name=sam]
*/

Complexity of push and pop Employee object in a stack in java>
Complexity of insertion and deletion at first is O(1) in java.
As we have to always insert at first - best case, average case and worst case are same in java.

So in this Data structures tutorial we learned how to push and pop Employee object in a stack in java with example and program. We used generic implementation of stack to achieve it in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

RELATED LINKS>
1) Stacks, Queues in Data Structures in java

2) Single LinkedList implementations in Data Structures in java:-


eEdit
Must read for you :