Java collection – Stack

The Stack (java.util.stack) class is a typical Stack data structure that can insert, read, and remove elements from the header. A Stack is an implementation of the List interface, but a Stack is rarely used as a List- unless you need to check all the elements currently stored in the Stack.

Note that the Stack class is a subclass of Vector, an ancient Java synchronization class that increases the overhead of calling all the methods in the Stack. In addition, Vector uses several older Java parts (no longer recommended) and supports Enumeration for Iterator, so use a Deque if you want to avoid this problem.

The Stack base

A Stack is a data structure, where you can add elements at the top, and you can remove elements from the top which is what we call LIFO atoms, whereas a Queue is FIFO, where you add elements from the tail and remove elements from the head.

Create a Stack

Create Stack instance:

Stack stack = new Stack();
Copy the code

Create the Stack using generics

When creating a Stack, you can specify a generic type:

Stack<String> stack = newStack<String>(); A Stack instance can contain only String instances.Copy the code

Stack add element

To create a Stack instance, you can add an element to the top of the Stack. The element must be a Stack object. Use push() to add an element:

Stack<String> stack = new Stack<String>();
 
stack.push("1");
Copy the code

The above example adds the String “1” to the top of the Stack.

Stack fetches and removes elements

If you add an element to the Stack, you can also remove it from it, using the pop() method:

Stack<String> stack = new Stack<String>();
 
stack.push("1");
 
String topElement = stack.pop();
Copy the code

Once the pop () method is called, the element is gone from the Stack.

Get the element from the top of the Stack

You can call the Stack’s peek() method to view the first element of the Stack without removing the element:

Stack<String> stack = new Stack<String>();
 
stack.push("1");
 
String topElement = stack.peek();
Copy the code

After executing the code, the value of the topElement variable is “1” and still exists.

Search for the position of an element in the Stack

The Stack’s search() method is used to find the position of the element, which is compared using equals(). If the element is at the top, the index position value is 1:

Stack<String> stack = new Stack<String>();
 
stack.push("1");
stack.push("2");
stack.push("3");
 
int index = stack.search("3");     //index = 3
Copy the code

The size of the Stack

The Stack size() method can be used to obtain the number of elements in the Stack:

Stack<String> stack = new Stack<String>();
 
stack.push("1");
stack.push("2");
stack.push("3");
 
int size = stack.size();
Copy the code

The value of size after executing the code is 3.

Iterate over the elements in the Stack

We can get iterators from the Stack iterator() method to iterate over the Stack:

Stack<String> stack = new Stack<String>();
 
stack.push("123");
stack.push("456");
stack.push("789");
 
Iterator iterator = stack.iterator();
while(iterator.hasNext()){
    Object value = iterator.next();
}
Copy the code

The Stream process Stack

You can also handle elements on the Stream Api. The Stream is first fetched from the stack through the Stream () method. Once you get the Stream from the Stack, you can iterate over it:

Stack<String> stack = new Stack<String>();
stack.push("A");
stack.push("B");
stack.push("C");
 
Stream stream = stack.stream();
 
stream.forEach((element) -> {
    System.out.println(element);  // print element
});
Copy the code

Use the Stack to reverse the List

First add all the elements in the List to the List, then empty the List, then iterate over the Stack, remove the elements from the Stack and add them to the List:

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list);
 
Stack<String> stack = new Stack<String>();
while(list.size() > 0) {
    stack.push(list.remove(0));
}
 
while(stack.size() > 0){
    list.add(stack.pop());
}
 
System.out.println(list);
Copy the code

Use a Deque as a Stack

We can use the Deque as a Stack, adding and fetching elements from the header:

Deque<String> dequeAsStack = new ArrayDeque>String>();
 
dequeAsStack.push("one");
dequeAsStack.push("two");
dequeAsStack.push("three");
 
String one   = dequeAsStack.pop();
String two   = dequeAsStack.pop();
String three = dequeAsStack.pop();
Copy the code