Java Stack
source link: https://www.tuicool.com/articles/hit/I7bmEfj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The Java Stack
class, java.util.Stack
, is a classical stack data structure.
You can push elements to the top of a Java
Stack
and pop them again, meaning read and remove
the elements from the top of the stack.
The Java Stack
class actually implements theJava List interface,
but you rarely use a Stack
as a List
- except perhaps if you need to inspect
all elements currently stored on the stack.
Java Stack Basics
A Stack
is a data structure where you add elements to the "top" of the stack, and also remove
elements from the top again. This is also referred to as the "Last In First Out (LIFO)" principle. In contrast, aJava Queue uses a "First In First Out (FIFO)" principle, where elements are added
to the end of the queue, and removed from the beginning of the queue.
Create a Stack
To use a Java Stack
you must first create an instance of the Stack
class.
Here is an example of creating a Java Stack
instance:
Stack stack = new Stack();
Push Element on Stack
Once you have a Java Stack
instance, you can push elements to the top of the Stack
.
The elements you push onto the Stack
must be Java objects. Thus, you actually push objects
to the Stack
.
You push elements onto a Java Stack
using its push()
method.
Here is an example of pushing an element (object) onto a Java Stack
:
Stack stack = new Stack(); stack.push("1");
This Java example pushes aJava String with the text 1
onto the Stack
. The String 1
is then stored at the top of the Stack
.
Pop Element From Stack
Once an element has been pushed onto a Java Stack
, you can pop that element from the Stack
again. Once an element is popped off the Stack
, the element is removed
from the Stack
. The top element of the Stack
is then whatever element that was
pushed onto the Stack
just before the element just popped.
You pop an element off a Java Stack
using the pop()
method. Here is an example of
popping an element off a Stack
using the pop()
method:
Stack stack = new Stack(); stack.push("1"); String topElement = stack.pop();
Once an element is popped off a Stack
, the element is no longer present on the Stack
.
Peek at Top Element of Stack
The Java Stack
class has a method called peek()
which enables you to see what the top
element on the Stack
is, without popping off the element. Here is an example of peeking at the top
of a Java Stack
:
Stack stack = new Stack(); stack.push("1"); String topElement = stack.peek();
After running this Java example the topElement
variable will contain the String object 1
which was pushed onto the Stack
just before peek()
was called. The String object
is still present on the Stack
after calling peek()
.
Search the Stack
You can search for an object on the stack to get it's index, using the search()
method. The object's equals()
method is called on every object on the Stack
to determine if the searched-for object is present on the Stack
. The index you get
is the index from the top
of the Stack
, meaning the top element on the Stack
has index 1.
Here is how you search a Stack
for an object:
Stack stack = new Stack(); stack.push("1"); stack.push("2"); stack.push("3"); <b>int index = stack.search("3");</b> //index = 1
Stack Use Cases
A Stack
is really handy for some types of data processing,
for instance if you are parsing an XML file using eitherSAX orStAX. For an example, see myJava SAX Example in my Java XML tutorial.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK