java集合ArrayDeque、LinkList、Stack分别分析与区别(最新)

ArrayDeque基本特征

public class ArrayDeque<E> extends AbstractCollection<E>
                   implements Deque<E>, Cloneable, Serializable

1.实现了Deque使其具有作为双端队列和作为栈的能力。
2.实现了Cloneable它可克隆。
3.实现了Serializable表明可序列化。
4.(没有实现RandomAccess,表明不支持随机访问)。
5.使用循环数组(后面会提到)
ArrayDeque与早期继承与Vector的Stack不同,ArrayDeque是非线程安全的,不支持并发修改(只能在外部进行同步),支持快速失败

LinkList基本特征

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

1.实现了List接口,表明LinkList具有随机存取的能力
2.实现了Deque使其具有作为双端队列和作为栈的能力。
3.实现了Cloneable它可克隆。
4.实现了Serializable表明可序列化。
5.使用双向链表
LinkList是非线程安全的,不支持并发修改,支持快速失败

ArrayDeque代码分析

默认容量为16

    public ArrayDeque() {
        elements = new Object[16];
    }

容量只能为2的次幂

   public ArrayDeque(int numElements) {
        allocateElements(numElements);
    }
      private void allocateElements(int numElements) {
        elements = new Object[calculateSize(numElements)];
    }
        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;
    }

添加元素

添加元素采用移动head或tail指针的方式,使得添加元素过程的复杂度为O(0)。
容量为2的次幂的原因是元素添加的方式(元素添加时使用head-1和length-1相与的形式来计算,在这个运算中必须保证length-1的结果二进制表示必须是全1序列。)。

    /**
     * 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)
            throw new NullPointerException();
        elements[head = (head - 1) & (elements.length - 1)] = e;
        if (head == tail)
            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)
            throw new NullPointerException();
        elements[tail] = e;
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
            doubleCapacity();
    }

同时也可以由addFirst添加元素的方式知道:ArrayDeque使用的是循环数组。

删除元素

删除元素采用移动head或tail指针的方式,使得删除元素过程的复杂度为O(0)。

 public E pollFirst() {
        int h = head;
        @SuppressWarnings("unchecked")
        E result = (E) elements[h];
        // Element is null if deque empty
        if (result == null)
            return null;
        elements[h] = null;     // Must null out slot
        head = (h + 1) & (elements.length - 1);
        return result;
    }

    public E pollLast() {
        int t = (tail - 1) & (elements.length - 1);
        @SuppressWarnings("unchecked")
        E result = (E) elements[t];
        if (result == null)
            return null;
        elements[t] = null;
        tail = t;
        return result;
    }

扩容方式为增大到原来的两倍

将原数组中元素复制到新数组中,第一个元素下标为0,最后一个元素下标为原数组长度。

    /**
     * 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
        int newCapacity = n << 1;
        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;
    }

LinkList代码分析

LinkList类中维护一个size,两个Node指针first和last:

transient int size = 0;
transient Node<E> first;
transient Node<E> last;

内部类Node的定义如下:

    private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

表明LinkList使用的是双向链表的存储结构。

添加元素

1.首尾添加

对外提供的添加首尾元素的方法都调用如下两个方法实现添加功能(如addFirst(E e) 方法):

 /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }
   /**
     * Links e as first element.
     */
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    /**
     * Links e as last element.
     */
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

在双向链表的存储结构下,首尾元素添加都能获得O(0)的时间复杂度,

2.随机添加

对外提供的随机添加元素的方法都调用如下方法实现添加功能(如add(int index, E element) 方法):

//在随机位置添加元素的方法
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }
    
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }
        /**
     * Returns the (non-null) Node at the specified element index.
     */
    Node<E> node(int index) {
        // assert isElementIndex(index); 
        
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

随机添加过程:
1.检查参数传递的下标index是否合法(在[0,size]范围内:index >= 0 && index <= size;
2.判断添加位置是否为双链表的尾部(index == size)如果为尾部就调用尾部添加元素的方法(linkLast),
3.如果不是添加到尾部,调用node(int index)方法找到待添加节点的后继,将待添加元素和node方法返回的节点作为参数传递给linkBefore(E e, Node<E> succ)完成添加功能。
node(int index)方法说明:该方法返回查找指定下标位置的节点,通过将双链分为前后两部分(size >> 1)来提高查找的效率,使查找时间减半。

删除元素

1.首尾删除

对外提供的删除首尾元素的方法都调用如下两个方法实现添加功能(如removeFirst() 和removeLast() 方法):

    /**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }
    /**
     * Removes and returns the last element from this list.
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
        /**
     * Unlinks non-null first node f.
     */
    private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }
        /**
     * Unlinks non-null last node l.
     */
    private E unlinkLast(Node<E> l) {
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }

头部删除(removeFirst())和尾部删除(removeLast())都在进入实际删除节点的方法前(unlinkFirst(Node<E> f)unlinkLast(Node<E> l))对节点进行了空检查,实际删除节点的工作通过将节点与双链断开的方式完成。删除的方法将被删除的元素值作为返回值返回。

2.随机位置删除

随机位置删除通过调用node节点方法查找到合适待删除节点,然后调用方法完成删除功能。

    /**
     * Removes the element at the specified position in this list.  Shifts any
     * subsequent elements to the left (subtracts one from their indices).
     * Returns the element that was removed from the list.
     *
     * @param index the index of the element to be removed
     * @return the element previously at the specified position
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }
        /**
     * Unlinks non-null node x.
     */
    E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }

实际删除节点的方法E unlink(Node<E> x)也是通过将节点与双链断开的方式完成。

Stack基本特征

public
class Stack<E> extends Vector<E> 

Stack的在Vector的基础上添加的栈的几个方法,这几个方法被synchronized关键字修饰,使得Stack具有了同步功能。

ArrayDeque、LinkList和Stack的比较

ArrayDeque和LinkList性能分析

1.在头尾插入节点的性能:ArrayDeque使用循环数组存储,元素的添加和删除采用移动head和tail的方式实现效率很好(时间复杂度为O(0)),但是数组存在扩容过程对效率有一定影响。 LinkList使用双链表存储数据,虽然元素的添加和删除时间复杂度也是O(0),并且没有扩容的时间开销,但是双链表的元素添加过程每次都会创建新的节点,使得其插入插入效率低于ArrayDeque。
2.在头尾删除节点的性能:ArrayDeque和LinkList在头尾删除节点都具有O(0)的时间复杂度,性能上“相差不多”(引用)。
结论:ArrayDeque性能总体优于LinkList

ArrayDeque和Stack比较

Stack继承自Vector,由于Vector使用简单数组存储结构,并不是像ArrayDeque那样的循环数组存储结构,导致Stack的性能比ArrayDeque差很多(主要由于删除元素时Stack的实现方式是前移待删除元素之后的所有元素,时间复杂度为O(n)),而ArrayDeque是通过更改头指针来实现。
结论:在实现栈时,ArrayDeque性能优于Stack。如果需要同步ArrayDeque可以“在外部进行同步”(JDK1.8文档)。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值