JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Stack implementation using generics bound type.


What happens in case we want to create a generic stack which should allow us to create a stack for any kind of object. For instance, I want to create a stack which should accept only string objects. Similarly I want to create a stack which accepts only integers. How can I fulfill this requirement? We can implement this using generics bound type. Using this you can add group of objects from the same family as well. Incase if your stack wants to allow the objects extended by a super class, then you need to replace Object with your super class at line no:3 , in the below example.


package com.java2novice.ds.stack;

public class MyGenericsStack<T extends Object> {

	private int stackSize;
	private T[] stackArr;
	private int top;
	
	/**
	 * constructor to create stack with size
	 * @param size
	 */
	@SuppressWarnings("unchecked")
	public MyGenericsStack(int size) {
		this.stackSize = size;
		this.stackArr = (T[]) new Object[stackSize];
		this.top = -1;
	}

	/**
	 * This method adds new entry to the top 
	 * of the stack
	 * @param entry
	 * @throws Exception 
	 */
	public void push(T entry){
		if(this.isStackFull()){
			System.out.println(("Stack is full. Increasing the capacity."));
			this.increaseStackCapacity();
		}
		System.out.println("Adding: "+entry);
		this.stackArr[++top] = entry;
	}

	/**
	 * This method removes an entry from the 
	 * top of the stack.
	 * @return
	 * @throws Exception 
	 */
	public T pop() throws Exception {
		if(this.isStackEmpty()){
			throw new Exception("Stack is empty. Can not remove element.");
		}
		T entry = this.stackArr[top--];
		System.out.println("Removed entry: "+entry);
		return entry;
	}
	
	/**
	 * This method returns top of the stack
	 * without removing it.
	 * @return
	 */
	public T peek() {
		return stackArr[top];
	}

	private void increaseStackCapacity(){
		
		@SuppressWarnings("unchecked")
		T[] newStack = (T[]) new Object[this.stackSize*2];
		for(int i=0;i<stackSize;i++){
			newStack[i] = this.stackArr[i];
		}
		this.stackArr = newStack;
		this.stackSize = this.stackSize*2;
	}
	
	/**
	 * This method returns true if the stack is 
	 * empty
	 * @return
	 */
	public boolean isStackEmpty() {
		return (top == -1);
	}

	/**
	 * This method returns true if the stack is full
	 * @return
	 */
	public boolean isStackFull() {
		return (top == stackSize - 1);
	}
	
	public static void main(String a[]){
		MyGenericsStack<String> stringStack = new MyGenericsStack<String>(2);
		stringStack.push("java2novice");
		MyGenericsStack<Integer> integerStack = new MyGenericsStack<Integer>(2);
		integerStack.push(23);
	}
}

Output:
Adding: java2novice
Adding: 23
<< Previous Program | Next Program >>
blog comments powered by Disqus

List of Stack Data Structure Examples

  1. Stack introduction & implementation
  2. Java Dynamic Stack Implementation
  3. Stack implementation using generics bounded type.
  4. Reverse a word or string using Stack data structure.
  5. Write a program to find out delimiter matching using stack.
  6. Convert a decimal into a binary number using stack.
  7. Towers of Hanoi implementation using stack.
  8. Evaluation of an infix expression that is fully parenthesized using stack in java.
Knowledge Centre
Class, Constructor and Primitive data types
Class is a template for multiple objects with similar features and it is a blue print for objects. It defines a type of object according to the data the object can hold and the operations the object can perform. Constructor is a special kind of method that determines how an object is initialized when created. Primitive data types are 8 types and they are: byte, short, int, long, float, double, boolean, char.
Famous Quotations
You can never get enough of what you don’t really need.
-- Eric Hoffer

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.