Friday, 26 June 2015

Java : Collection Framework : Stack peek


Click here to watch in Youtube :
https://www.youtube.com/watch?v=kkRDmV0dIDs&list=UUhwKlOVR041tngjerWxVccw

StackExample.java
import java.util.Stack;

/*
 Method 

 public E peek()

 Returns:
 
 the object at the top of this stack (the last item of the Vector object).
 
 Throws:
 
 EmptyStackException - if this stack is empty.

 */

public class StackExample
{

    public static void main(String[] args)
    {
        Stack<String> stack = new Stack<String>();

        stack.push("Apple");
        stack.push("Ball");
        stack.push("Cat");
        stack.push("Dog");
        
        /*
         * [
         *  Dog
         *  Cat
         *  Ball
         *  Apple
         * ]
         */

        System.out.println("Before peek() method is called : stack : " + stack + "\n");

        /*
         * Looks at the object at the top of this stack without removing it from
         * the stack.
         */
        String object = stack.peek();

        System.out.println("object : " + object + "\n");

        System.out.println("After peek() method is called : stack : " + stack);

    }

}
Output
Before peek() method is called : stack : [Apple, Ball, Cat, Dog]

object : Dog

After peek() method is called : stack : [Apple, Ball, Cat, Dog]
To Download StackDemoPeekApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/StackDemoPeekApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge