【JDK1.8源码阅读】Stack

4 篇文章 0 订阅

JDK1.8-Stack

前言

Stack是一个最后进最先出的栈(last-in-first-out,LIFO)对象。它是继承自Vector类的5个,允许vector被当做栈一样处理的操作方法。通用的pushpop方法被提供,还有peek栈的顶部元素,测试栈是否为emptysearch一个元素并发现它距离栈顶有多远。

当栈第一次被创建时,它里面是没有元素的。

Deque接口和它的实现类,提供了完全一致的LIFO栈操作,应该优先考虑用它,而不是栈。

例如:Deque stack = new ArrayDeque();

该类在JDK1.0的时候就有了,作者Jonathan Payne。

源码阅读

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

    /**
     * 向当前栈顶压入一个元素,它调用的是vector的addElement:
     * 向数组的最后添加一个元素
     * @param   item   the item to be pushed onto this stack.
     * @return  the <code>item</code> argument.
     * @see     java.util.Vector#addElement
     */
    public E push(E item) {
        // 该方法是vector提供,并被synchronized修饰
        addElement(item);

        return item;
    }

    /**
     * 拿到栈顶元素,并将其从栈中删除,拿到的元素作为方法返回值
     * 由于该方法会改变栈的结构(增、删),为保证单机多线程安全,给方法加了锁
     * 锁的是当前的栈对象,这样就可以避免数据未更新就被读取或使用,同样其他方法也加了锁。
     * @return  The object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        // 获得栈的大小,该方法由Victor提供,且是synchronized修饰
        int     len = size();
		// 查询得到栈顶元素
        obj = peek();
        // 移除栈顶元素(栈的最后一个元素)
        removeElementAt(len - 1);

        return obj;
    }

    /**
     * 查询栈顶元素并返回
     *
     * @return  the object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        // 返回最后一个元素,该方法由Victor提供,且是synchronized修饰
        return elementAt(len - 1);
    }

    /**
     * 判断栈是否为空,主要是通过size,栈的大小来判断,size为0表示没有元素,即为空。
     *
     * @return  <code>true</code> if and only if this stack contains
     *          no items; <code>false</code> otherwise.
     */
    public boolean empty() {
        // 该方法由Victor提供,且是synchronized修饰
        return size() == 0;
    }

    /**
     * 返回指定元素距离栈顶的长度,这个长度是以1为基础的,即如果栈顶元素就是你所要
     * 查询的元素,那么返回的是1。在栈中判断两个元素相等时,使用的是`equals`方法。
     *
     * @param   o   the desired object.
     * @return  the 1-based position from the top of the stack where
     *          the object is located; the return value <code>-1</code>
     *          indicates that the object is not on the stack.
     */
    public synchronized int search(Object o) {
        // 找到第一次出现该元素的位置,该方法由Victor提供,且是synchronized修饰
        // 该方法是从数组最后(栈顶)开始往前找
        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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值