All About Class Stack in Java

Stack in Java
Stack in Java

Stack in Java

The Stack class extends Vector class.In addition to inherited methods from vector class,the Stack class contains some more extra methods.The Stack class represents a container that implements Last In First Out (LIFO) or First In Last Out(FILO) structure of Objects in Java.That is whenever we push some object to the stack,the last is the first item we can pop out.Like other containers,we can push and pop objects.So we must cast to appropriate type when we pop an object from Stack.

The structure of Stack class is given below:


public class java.util.stack extends Java.util.vector{
//constructor
public stack();
//creates a stack with no elements in it.
//Methods
public boolean empty();//returns true if the stack is empty.
public Object peek();//returns the element at the top of the stack.
public Object pop();//removes the top element of the Stack and returns the element that is removed.
public Object push(Object item);//adds the item to the top of the stack.
public int search(Object itemToSearch);  
}

An Example:


import java.util.*;
public class StackTest{
     public static void main(String []args){
        Stack st=new Stack();
        st.push("hello");
        st.push("hi");
        st.push("Good Morning");
        System.out.println("The elements of the Stack "+st);
        System.out.println("The element at the top of the Stack "+st.peek());
        System.out.println("The element popped out of the Stack "+st.pop());
        System.out.println("The elements of the Stack after pop "+st);
        System.out.println("The result of search from the Stack "+st.search("hi"));
        //search() will return -1 if not found
     }
}
 
The output of the code:
$javac StackTest.java
Note: StackTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M StackTest
The elements of the Stack [hello, hi, Good Morning]
The element at the top of the Stack Good Morning
The element popped out of the Stack Good Morning
The elements of the Stack after pop [hello, hi]
The result of search from the Stack 1

Another example:


import java.util.*;
public class StackTest{
     public static void main(String []args){
     String pathOfFile="C:\Test\abc.txt"
     BufferedReader in =new BufferedReader(new FileReader(pathOfFile))
//To read a line at a time from a Stream , we need to create a BufferedReader 
//object and to open a file we need to create FileReader object.
     Stack st=new Stack();
     String s;
//Once the BufferedReader object is created we can read file line by line by calling
//readLine() method.It breaks when end of file is reached.
     while((s=in.readLine())!=null)
pushing to stack
     st.push("\n"+s);
//each line in the file is read and pushed to the stack.
     System.out.println("st="+st);
//treating stack as vector,it can be done without a cast.As stack is a Vector class
    Vector v=st;
//we can perform all the vector related operations here
    System.out.println("3rd element="+v.elementAt(2));
//poping from stack
    while((!st.empty())
    System.out.println("Poping="+st.pop());
}
 

All About Class Stack in Java All About Class Stack in Java Reviewed by Animesh Chatterjee on September 06, 2018 Rating: 5

No comments:

Powered by Blogger.