Stack
A Stack
is a linear data structure that adds and removes items from the top in a last-in, first-out (LIFO) order. The Stack
class comes from the java.util
package and extends the legacy Vector
class.
An opposite data structure is the Queue
interface, which follows the first-in, first-out (FIFO) order. The Stack
and Queue
concepts can be observed in everyday life (e.g., waiting in a line of people to purchase something or washing a stack of dishes from top to bottom).
Since Java 1.6, there is a more recent implementation of LIFO stack operations, the Deque
interface. As stated in the JDK documentation, Deque
interface should be used in preference to the legacy Stack
class. Whenever a Deque
is used as a Stack
, elements are pushed and popped from the beginning of the Deque
. The table below shows the equivalent methods between Stack
and Deque
:
Stack method |
Equivalent Deque method |
---|---|
.push(item) |
.addFirst(item) |
.pop() |
.removeFirst() |
.peek() |
.getFirst() |
Syntax
import java.util.Stack;
Stack<DataType> s = new Stack<>();
Where DataType
is the data type to be stored in the stack.
Note: Unlike Queue
, Stack
is a concrete class and not an interface.
Methods
The Stack
class provides the following methods:
.push(item)
: adds an item to the top of theStack
..pop()
: removes and returns the object at the top of theStack
, throwing an exception when theStack
is empty..peek()
: returns the (top) of theStack
without removing it, and throws an exception when theStack
is empty..empty()
: returnstrue
if theStack
contains no items orfalse
, otherwise..search(item)
: returns the distance theitem
is from the top of theStack
, starting from1
, or-1
is ifitem
is not in theStack
.
Example
The following example demonstrates the Stack
class:
// Main.javaimport java.util.Stack;import java.util.Arrays;public class Main {public static void main(String[] args) {Stack<String> books = new Stack<>();System.out.println(books.isEmpty());books.push("Effective Java");books.push("Head First Java");books.push("Thinking in Java");System.out.println(books.size());System.out.println(books.search("Effective Java"));System.out.println(books.search("Java for dummies"));System.out.println(books.peek());System.out.println(books.pop());System.out.println(books.size());System.out.println(Arrays.toString(books.toArray()));}}
This will output the following:
true33-1Thinking in JavaThinking in Java2[Effective Java, Head First Java]
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly16 hours