Java collection Stack example

Java collection Stack example

How to use Stack class in java collection?

View Answers

June 22, 2012 at 3:00 PM

The Stack class uses the First In Last Out(FILO) mechanism. Here is an example of Stack class.

import java.util.Stack;

public class StackExample {
    public static void main(String [] args){
        Stack stack = new Stack();
        stack.push("Angelina Jolie");
        stack.push("Jennifer Aniston");
        stack.push("Scarlett Johansson");
        stack.push("Reese Witherspoon");
        stack.push("Megan Fox");
        stack.push("Nicole Kidman");
        System.out.println("The elements in the Stack :- " + stack);
        System.out.println("The element at the top :- " +stack.peek());
        System.out.println("The element poped out of the stack :- " + stack.pop());
        System.out.println("The element in a stack after pop out an element- : " +stack);
        System.out.println("The result of searching :- " +stack.search(" r u"));
    }


}

Output:-

The elements in the Stack :- [Angelina Jolie, Jennifer Aniston, Scarlett Johansson, Reese Witherspoon, Megan Fox, Nicole Kidman]
The element at the top :- Nicole Kidman
The element poped out of the stack :- Nicole Kidman
The element in a stack after pop out an element- : [Angelina Jolie, Jennifer Aniston, Scarlett Johansson, Reese Witherspoon, Megan Fox]
The result of searching :- -1

Description:- The above example demonstrates you the Stack class in java collection. We are using three methods of Stack. push(object items): The push method add an items to the top of the Stack. peek(): The peek() method returns the element at the top of the Stack, and pop(): The pop() method remove the element at the top of the Stack and returns the element.









Related Tutorials/Questions & Answers:
Java collection Stack example
:- The above example demonstrates you the Stack class in java collection. We...Java collection Stack example  How to use Stack class in java collection?   The Stack class uses the First In Last Out(FILO) mechanism
Collection : Stack Example
Collection : Stack Example In this tutorial we will describe implementation of stack with example. Stack : Stack is based on concept of LIFO(Last In First... if stack is empty. Some other methods - empty(), peek(), search(Object o) Example
Advertisements
Java Stack Example
Java Stack Example In this section we will read about how you can provide... will demonstrate you about how to use the stack collection in the Java programming... Collection. Stack may have the two states while pushing and popping is occurred
Java collection Queue Example
Java collection Queue Example  How can we use Queue in java collection?   import java.util.LinkedList; import java.util.Queue; public... LinkedList(); queue.add("Java"); queue.add("DotNet
Java Stack Example
Java Stack Example In this section you will learn about Stack class  in java, how to use Stack in java. Before going into details we should know what... the item in the stack and how far it is from top of stack Example : A Java code
Java Collection iterator with example
. Example of Java Collection Iterator import java.util.ArrayList; import... The Java Collection Iterator is present at the highest level interface in the Collection framework. Iterator interface has methods for traversing
Example of Java Stack Program
Example of Java Stack Program     ... it. Here in the example describes the methods to prepare an example of Java stack...;} } Output of this example is given below: The contents of Stack
stack in java/
stack in java/  java code for stack push pop and display operation? thanks
Collection : ArrayList Example
This tutorial contains description of Collection ArrayList with example
Collection : LinkedList Example
This tutorial contains description of Collection LinkedList with example
stack and queue - Java Beginners
stack and queue  write two different program in java 1.) stack 2...://www.roseindia.net/java/example/java/util/StackImplement.shtml http://www.roseindia.net/java/example/java/util/QueueImplement.shtml Hope
Collection Iterate Example
Collection Iterate Example     ... of interface Iterator in Java. The description of the code is given below... the hasNext() method through the following java program. True is return
java technologies stack
java technologies stack  java technologies stack
Stack
Stack  How to implement a stack using classes in java?   Here is an example that implements a Stack class based on arrays. public class Stack { private int top; private int[] storage; Stack(int
Java Collection
Java Collection  What is the Collection interface
Implementing a Stack in Java
Implementing a Stack in Java     ... in Java. A Stack is like a bucket in which you can put elements one-by-one... entered element. Stack is a collection of data and follows the LIFO (Last
Java Collection : NavigableSet Example
Java Collection : NavigableSet Example In this tutorial we are describing NavigableSet of collection Framework NavigableSet : NavigableSet interface..., boolean inclusive). Example : Here is an example of NavigableSet . package
stack - Java Beginners
on Stack visit to : Thanks   http://www.roseindia.net/java/example/java/util/ Thanks  I think there is bit wrong in the above answer... that inputs a line of text and uses a stack object to print the words of the line
Java Collection : LinkedHashSet Example
Java Collection : LinkedHashSet Example This tutorial contains description of LinkedHashSet with example. LinkedHashSet : LinkedHashSet class is defined... of HashSet. Example : package collection; import java.util.LinkedHashSet; class
Java Collection : TreeSet Example
Java Collection : TreeSet Example This tutorial contains description of  TreeSet with example. TreeSet  : TreeSet class is defined in java.util... constructor - TreeSet(), TreeSet(Collection c), TreeSet(Comparator c
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection
Java Stack - Java Beginners
Java Stack  Can you give me a code using Java String STACK using the parenthesis symbol ( ) the user will be the one to input parenthesis......) if Open parenthesis push it to stack otherwise Pop the stack Thank you
Collection : ArrayList Example
Collection : ArrayList Example This tutorial contains description of Collection ArrayList with example. ArrayList : For ArrayList<E> import... of elements to add. For such condition, the Collection Framework provides
Java stack help
Java stack help  How would I use stacks in java to do a calculation like: 4*((3+3)/(5-3
Collection in java
Collection in java  What are the meaning of the letters E,T,K and V that come in the collection concept
Stack Overflow - Java Tutorials
Stack Overflow in Java A stack is the part of the memory. The local automatic... on due to which stack's limit exceeds. ADS_TO_REPLACE_1 For example, take... size greater than stack's limit. For example in the given below example
Generic Java Stack
Generic Java Stack  Hi here the question the interviewer asked me- package jp.co.worksap.intern; /** *The Stack class represents a last-in-first-out(LIFO) stack of objects *And this class can look at the object
Java Stack
Java Stack       The Stack class works on the principle last-in-first-out (LIFO) stack of objects... To know more about java stack click on the link: http:/www.roseindia.net/java
ADT stack - Java Beginners
ADT stack  Design a Java interface for ADT Stack. Develop two....  Hi Friend, Try the following codes: ADT Stack: import...("Stack is empty"); return; } else { String str = " "; for (int i = 0; i <= top
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
ADT stack - Java Beginners
]. 2)Design a Java interface for ADT Stack. Develop two different classes...ADT stack  I want a algorithm for the below programs.... 1)Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons
Java collection
Java collection  What is relation between map and set
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections
Java Collection
Java Collection  What are Vector, Hashtable, LinkedList and Enumeration
java collection
java collection  how do I use a treeset to print unique words
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java? whats the java program or code to that. Thank you rose india and java... the number: "); int num = input.nextInt(); Stack stack = new Stack
Filter collection in Java 8
Filter collection in Java 8  Hi, I have following collection..., (adsbygoogle = window.adsbygoogle || []).push({}); Example of Filter collection...; Hi, We have huge collection of Java util package examples check all at Java
Java garbage collection
Java garbage collection  What is Garbage Collection and how to call it explicitly
calculator in java with stack
calculator in java with stack  i want calcultor with interface in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
Java Collection API - Java Tutorials
Java Collection API Collection was added to Java with J2SE 1.2 release. Collection framework is provided in 'java.util.package'. All collections... example, the method "add" of Collection Interface is implemented
What are Java full stack technologies?
What are Java full stack technologies?  Hi, What are all...? What are Java full stack technologies? Is Java still popular in 2018... full stack. What are Java full stack technologies? Here is are the list of Java
Java collection-LinkedList
Java collection-LinkedList  How to use java collection-linked list ?   Example: import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class LinkedListExample{ public static void
Example to show Collection exception in java
Example to show Collection exception in java... of using exception class in java .Exceptions are the way in Java to indicate... describes  how to handle collection exceptions appropriately in your programs
Java collection -Hashtable
Java collection -Hashtable   What is Hashtable in java collection?   Java collection -Hashtable;- The hashtable is used to store value... is :Grapes The key is:B, value is :Orange Description:- The above example
Collection classes in java
is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project. Or the data is stored in both database and java collection classes
Collection classes in java
is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project. Or the data is stored in both database and java collection classes
garbage collection - Java Beginners
. For read more information : http://www.roseindia.net/java/example/java/io... of memory.It identifies the objects that are no longer used by a program. A Java object is subject to garbage collection when it becomes unreachable to the program
Java Collection-TreeSet
Java Collection-TreeSet  What is TreeSet in Java Collection?   Example: import java.util.Iterator; import java.util.TreeSet; public... of treeset using Iterator and display the elements.   Example: import
Java collection LinkedHashSet
Java collection LinkedHashSet  How can we use LinkedHashSet in java collection?   import java.util.Iterator; import... Description:- The above example demonstrates you the Set interface. Since Set
collection
collection  As we know array holds the similar kind of elements, then in collection how toArray() method will convert the collection having different objects as elements to an array in java

Ads