Wednesday, 19 October 2016

What is Stack In Java ?

Previously we have discussed about
Here we learn what is Stack In Java.But before that we must know, What is Collection Interface in Java ?
Class Stack<E>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

Declaration of Stack:
public class Stack<E>
extends Vector<E>

Stack class represents the elements in LIFO(Last in-First Out). It extends class Vector with five operations that allow a vector to be treated as a stack. 
The usual push and pop operations are provided, as well as a method to peek top item from the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

Constructor Summary of Stack:

Constructor
Description
Stack()
Creates an empty Stack.

Method Summary of Stack:

Modifier and Method name
Description
boolean empty()
Tests if this stack is empty.
E peek()
Looks at the object at the top of this stack without removing it from the stack.
E pop()
Removes the object at the top of this stack and returns that object as the value of this function.
E push(E item)
Pushes an item onto the top of this stack.
int search(Object o)
Returns the 1-based position where an object is on this stack.

Example of Stack:

import java.util.Stack;

public class StackClassExample {

public static void main(String[] args) {
Stack<String> st = new Stack<String>();
System.out.println("Initial Size of Stack :\t"+st.size());
//Adding elements in stack using push() method
st.push("A");
st.push("B");
st.push("C");
st.push("D");
System.out.println("Displaying Stack :\t"+st);
System.out.println();
System.out.println("pop(), This method removes the element from top of stack :\t"+st.pop());
System.out.println("peek(), This method gives u the top element from stack :\t"+st.peek());
System.out.println("search(B), This method search specified element :\t"+st.search("B"));
System.out.println("empty(), This method return true/false :\t"+st.empty());
System.out.println("\nStack after Operations :\t"+st);
System.out.println("Size of Stack after operation :\t"+st.size());
}

}
Program Output:

Initial Size of Stack : 0
Displaying Stack : [A, B, C, D]

pop(), This method removes the element from top of stack : D
peek(), This method gives u the top element from stack : C
search(B), This method search specified element : 2
empty(), This method return true/false : false

Stack after Operations : [A, B, C]
Size of Stack after operation : 3

   
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment