Home > Java > javaTutorial > body text

Introduction to the usage of stack in Java (with code)

不言
Release: 2019-03-29 11:13:01
forward
3222 people have browsed it

This article brings you an introduction to the usage of the stack in Java (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Stack in Java is an early legacy class. Sun/Oracle has deprecated its use and is now only reserved for compatibility with legacy code.

Legacy implementation

As shown below As shown in the code, the implementation in java.util.Stack is based on dynamic arrays, and Vector is also an abandoned class.

Personally, this implementation has two problems

It is based on Vector and requires synchronization, so the performance loss is serious

It is based on an array rather than a linked list, and the stack is very large It requires multiple expansions, causing unnecessary performance losses

public
class Stack<E> extends Vector<E> {
    /**
     * Creates an empty Stack.
     */
    public Stack() {
    }

    public E push(E item) {
        addElement(item);

        return item;
    }

    public synchronized E pop() {
        E       obj;
        int     len = size();

        obj = peek();
        removeElementAt(len - 1);

        return obj;
    }

    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }


    public boolean empty() {
        return size() == 0;
    }


    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = 1224463164541339165L;
}
Copy after login

My own simple encapsulation

The following is a Stack based on LinkedList encapsulation

The stack here is a decorator .

import java.util.LinkedList;
import java.util.NoSuchElementException;

public class Stack<T> {
    private LinkedList<T> stack;

    Stack() {
        stack = new LinkedList<>();
    }

    public void push(T o) {
        stack.add(o);
    }

    public T pop() {
        if (size() <= 0) {
            throw new NoSuchElementException("Stack is Empty.");
        }
        return stack.removeLast();
    }

    public T peek() {
        if (size() <= 0) {
            throw new NoSuchElementException("Stack is Empty.");
        }
        return stack.getLast();
    }

    public boolean empty() {
        return stack.size() == 0;
    }

    public int size() {
        return stack.size();
    }
}
Copy after login

This article has ended here. For more other exciting content, you can pay attention to the Java Video Tutorial column of the PHP Chinese website!

The above is the detailed content of Introduction to the usage of stack in Java (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template