欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java Review - Queue和Stack 源码解读

发布时间:2025/3/21 java 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Java Review - Queue和Stack 源码解读 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

  • Pre
  • 概述
  • Queue
  • Deque
  • ArrayDeque
    • 一览
    • 构造函数
    • 属性
    • 方法
      • addFirst()
      • addLast()
      • pollFirst()
      • pollLast()
      • peekFirst()
      • peekLast()


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)。
–抛出异常的方法带有返回值的方法
Insertadd(e)offer(e)
Removeremovepoll()
Examineelement()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大.


构造函数

/*** 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 <i>front</i> 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);} /*** 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)];} /*** 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 offinitialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements}return initialCapacity;}

三个构造函数

  • 申请默认大小为16的数组
  • 提供需要空间大小的有参构造器:利用allocateElements申请空间
  • 利用现有集合的有参构造器:同样利用allocateElements申请空间,再将现有集合中的元素拷贝到数组中
  • allocateElements(int numElements) :数组最小空间为8, 如果需要空间小于8,则申请数组大小为8,如果需要空间大于等于8,进行一定的容量扩大,而不只是提供需要数量的空间,防止下一次操作时又要进行扩容


    属性

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

    • head指向队列的头,tail指向队列尾的下一个位置,队列满的条件就是head == tail.

    • 扩容操作的执行时机:每次在向队列中添加元素以后,不论是在头部还是在尾部添加。

    • 扩容策略:空间是原空间的两倍大,将原来数组中元素拷贝到新数组中,因为是循环队列,可能出现head在tail后面的情况,拷贝到新数组时,从head指向开始拷贝,直到tail,也就是说,拷贝完成后,head指向新数组起始位置,tail指向最后一个元素的下一个位置。


    方法

    addFirst()

    /*** 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) //不允许放入nullthrow 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的补码。

    接下来看扩容的逻辑

    /*** 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()进行扩容。

    /*** Inserts the specified element at the end of this deque.** <p>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) //不允许放入nullthrow new NullPointerException();elements[tail] = e; //赋值if ( (tail = (tail + 1) & (elements.length - 1)) == head) //下标越界处理doubleCapacity();//扩容}

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


    pollFirst()

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

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

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


    pollLast()

    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 worktail = t;return result; }

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


    peekFirst()

    public E peekFirst() {return elements[head]; // elements[head] is null if deque empty }

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


    peekLast()

    public E peekLast() {return elements[(tail - 1) & (elements.length - 1)]; }

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

    总结

    以上是生活随笔为你收集整理的Java Review - Queue和Stack 源码解读的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。