前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java Review - Queue和Stack 源码解读

Java Review - Queue和Stack 源码解读

作者头像
小小工匠
发布2021-11-15 15:27:50
2580
发布2021-11-15 15:27:50
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构


Pre

Java Review - ArrayList 源码解读

Java Review - LinkedList源码解读


概述

Java中有Stack类,却没有叫做Queue的类,它是个接口的名字。当需要使用栈时,Java已不推荐使用Stack,而是推荐使用更高效的ArrayDeque;

既然Queue只是一个接口,当需要使用队列时也就首选ArrayDeque了,次选LinkedList。


Queue

Queue接口继承自Collection接口,除了最基本的Collection的方法之外,它还支持额外的insertion, extraction和inspection操作。

这里有两组格式,共6个方法,

  • 一组是抛出异常的实现;
  • 另外一组是返回值的实现(没有则返回null)。

抛出异常的方法

带有返回值的方法

Insert

add(e)

offer(e)

Remove

remove

poll()

Examine

element()

peek


Deque

  • Deque是"double ended queue", 表示双向的队列,英文读作"deck".
  • Deque 继承自 Queue接口,除了支持Queue的方法之外,还支持insert, remove和examine操作
  • 由于Deque是双向的,所以可以对队列的头和尾都进行操作 . 同时也支持两组格式,一组是抛出异常的实现;另外一组是返回值的实现(没有则返回null)。共12个方法如下:
  • 当把Deque当做FIFO的queue来使用时,元素是从deque的尾部添加,从头部进行删除的; 所以deque的部分方法是和queue是等同的。如下
  • Deque的含义是“double ended queue”,即双端队列,它既可以当作栈使用,也可以当作队列使用。下表列出了Deque与Queue相对应的接
  • Deque与Stack对应的接口如下:

上面两个表共定义了Deque的12个接口。

添加,删除,取值都有两套接口,它们功能相同,区别是对失败情况的处理不同。

一组接口遇到失败就会抛出异常 另一组遇到失败会返回特殊值(false或null)。 除非某种实现对容量有限制,大多数情况下,添加操作是不会失败的。虽然Deque的接口有12个之多,但无非就是对容器的两端进行操作,或添加,或删除,或查看。

ArrayDeque

一览

ArrayDeque和LinkedList是Deque的两个通用实现,由于官方更推荐使用AarryDeque用作栈和队列,着重讲解ArrayDeque的具体实现。

  • 从名字可以看出ArrayDeque底层通过数组实现,为了满足可以同时在数组两端插入或删除元素的需求,该数组还必须是循环的,即循环数组(circular array),也就是说数组的任何一点都可能被看作起点或者终点。
  • ArrayDeque是非线程安全的(not thread-safe),当多个线程同时使用的时候,需要手动同步;
  • ArrayDeque不允许放入null元素

上图中我们看到,head指向首端第一个有效元素,tail指向尾端第一个可以插入元素的空位。因为是循环数组,所以head不一定总等于0,tail也不一定总是比head大.


构造函数

代码语言:javascript
复制
/**
     * Constructs an empty array deque with an initial capacity
     * sufficient to hold 16 elements.
     */
    public ArrayDeque() {
        elements = new Object[16];
    }

    /**
     * Constructs an empty array deque with an initial capacity
     * sufficient to hold the specified number of elements.
     *
     * @param numElements  lower bound on initial capacity of the deque
     */
    public ArrayDeque(int numElements) {
        allocateElements(numElements);
    }

    /**
     * Constructs a deque containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.  (The first element returned by the collection's
     * iterator becomes the first element, or front of the
     * deque.)
     *
     * @param c the collection whose elements are to be placed into the deque
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayDeque(Collection<? extends E> c) {
        allocateElements(c.size());
        addAll(c);
    }
代码语言:javascript
复制
   /**
     * Allocates empty array to hold the given number of elements.
     *
     * @param numElements  the number of elements to hold
     */
    private void allocateElements(int numElements) {
        elements = new Object[calculateSize(numElements)];
    }
代码语言:javascript
复制
    /**
     * The minimum capacity that we'll use for a newly created deque.
     * Must be a power of 2.
     */
    private static final int MIN_INITIAL_CAPACITY = 8;

    // ******  Array allocation and resizing utilities ******

    private static int calculateSize(int numElements) {
        int initialCapacity = MIN_INITIAL_CAPACITY;
        // Find the best power of two to hold elements.
        // Tests "<=" because arrays aren't kept full.
        if (numElements >= initialCapacity) {
            initialCapacity = numElements;
            initialCapacity |= (initialCapacity >>>  1);
            initialCapacity |= (initialCapacity >>>  2);
            initialCapacity |= (initialCapacity >>>  4);
            initialCapacity |= (initialCapacity >>>  8);
            initialCapacity |= (initialCapacity >>> 16);
            initialCapacity++;

            if (initialCapacity < 0)   // Too many elements, must back off
                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
        }
        return initialCapacity;
    }

三个构造函数

  1. 申请默认大小为16的数组
  2. 提供需要空间大小的有参构造器:利用allocateElements申请空间
  3. 利用现有集合的有参构造器:同样利用allocateElements申请空间,再将现有集合中的元素拷贝到数组中

allocateElements(int numElements) :数组最小空间为8, 如果需要空间小于8,则申请数组大小为8,如果需要空间大于等于8,进行一定的容量扩大,而不只是提供需要数量的空间,防止下一次操作时又要进行扩容


属性

ArrayDeque提供了两个变量来操作数组:head 、 tail.

  • head指向队列的头,tail指向队列尾的下一个位置,队列满的条件就是head == tail.
  • 扩容操作的执行时机:每次在向队列中添加元素以后,不论是在头部还是在尾部添加。
  • 扩容策略:空间是原空间的两倍大,将原来数组中元素拷贝到新数组中,因为是循环队列,可能出现head在tail后面的情况,拷贝到新数组时,从head指向开始拷贝,直到tail,也就是说,拷贝完成后,head指向新数组起始位置,tail指向最后一个元素的下一个位置。

方法

addFirst()

代码语言:javascript
复制
    /**
     * Inserts the specified element at the front of this deque.
     *
     * @param e the element to add
     * @throws NullPointerException if the specified element is null
     */
    public void addFirst(E e) {
        if (e == null) //不允许放入null
            throw new NullPointerException();
        elements[head = (head - 1) & (elements.length - 1)] = e; //2.下标是否越界
        if (head == tail)//1.空间是否够用
            doubleCapacity();//扩容
    }

addFirst(E e)的作用是在Deque的首端插入元素,也就是在head的前面插入元素,在空间足够且下标没有越界的情况下,只需要将elements–head = e即可 。

上述代码我们看到,空间问题是在插入之后解决的,因为tail总是指向下一个可插入的空位,也就意味着elements数组至少有一个空位,所以插入元素的时候不用考虑空间问题。

下标越界的处理 ,head = (head - 1) & (elements.length - 1)就可以了,这段代码相当于取余,同时解决了head为负值的情况。

因为elements.length必需是2的指数倍,elements - 1就是二进制低位全1,跟head - 1相与之后就起到了取模的作用,如果head - 1为负数(其实只可能是-1),则相当于对其取相对于elements.length的补码。

接下来看扩容的逻辑

代码语言:javascript
复制
    /**
     * Doubles the capacity of this deque.  Call only when full, i.e.,
     * when head and tail have wrapped around to become equal.
     */
    private void doubleCapacity() {
        assert head == tail;
        int p = head;
        int n = elements.length;
        int r = n - p; // number of elements to the right of p  .head右边元素的个数
        int newCapacity = n << 1; //原空间的2倍
        if (newCapacity < 0)
            throw new IllegalStateException("Sorry, deque too big");
        Object[] a = new Object[newCapacity];
        System.arraycopy(elements, p, a, 0, r);//复制右半部分,对应下图中绿色部分
        System.arraycopy(elements, 0, a, r, p);//复制左半部分,对应下图中灰色部分
        elements = a;
        head = 0;
        tail = n;
    }

其逻辑是申请一个更大的数组(原数组的两倍),然后将原数组复制过去。

图中我们看到,复制分两次进行,第一次复制head右边的元素,第二次复制head左边的元素。


addLast()

则调用doubleCapacity()进行扩容。

代码语言:javascript
复制
    /**
     * Inserts the specified element at the end of this deque.
     *
     * This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     * @throws NullPointerException if the specified element is null
     */
    public void addLast(E e) {
        if (e == null) //不允许放入null
            throw new NullPointerException();
        elements[tail] = e; //赋值
        if ( (tail = (tail + 1) & (elements.length - 1)) == head) //下标越界处理
            doubleCapacity();//扩容
    }

addLast(E e)的作用是在Deque的尾端插入元素,也就是在tail的位置插入元素,由于tail总是指向下一个可以插入的空位,因此只需要elementstail = e;即可。插入完成后再检查空间,如果空间已经用光调用doubleCapacity()进行扩容。


pollFirst()

代码语言:javascript
复制
public E pollFirst() {
    E result = elements[head];
    if (result == null)//null值意味着deque为空
        return null;
    elements[h] = null;//let GC work
    head = (head + 1) & (elements.length - 1);//下标越界处理
    return result;
}

pollFirst()的作用是删除并返回Deque首端元素,也即是head位置处的元素。

如果容器不空,只需要直接返回elementshead即可,当然还需要处理下标的问题。由于ArrayDeque中不允许放入null,当elementshead == null时,意味着容器为空。


pollLast()

代码语言:javascript
复制
 public E pollLast() {
    int t = (tail - 1) & (elements.length - 1);//tail的上一个位置是最后一个元素
    E result = elements[t];
    if (result == null)//null值意味着deque为空
        return null;
    elements[t] = null;//let GC work
    tail = t;
    return result;
}

pollLast()的作用是删除并返回Deque尾端元素,也即是tail位置前面的那个元素。


peekFirst()

代码语言:javascript
复制
public E peekFirst() {
    return elements[head]; // elements[head] is null if deque empty
}

peekFirst()的作用是返回但不删除Deque首端元素,也即是head位置处的元素,直接返回elementshead即可


peekLast()

代码语言:javascript
复制
public E peekLast() {
    return elements[(tail - 1) & (elements.length - 1)];
}

peekLast()的作用是返回但不删除Deque尾端元素,也即是tail位置前面的那个元素。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-11-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Pre
  • 概述
  • Queue
  • Deque
  • ArrayDeque
    • 一览
      • 构造函数
        • 属性
          • 方法
            • addFirst()
            • addLast()
            • pollFirst()
            • pollLast()
            • peekFirst()
            • peekLast()
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档