java.util LinkedList相关类解读

  1. 实现接口


    java.util LinkedList相关类解读_第1张图片
    LinkedList继承关系

看上图,LinkedListArrayList稍微有不一样的东西,那就是RandomAccessDeque,实现了双端队列,ArrayList实现RandomAccess遍历,快速访问,接口中说理论for优于Iterator迭代。所以快速访问,查找ArrayList优于LinkedList,but增删反之。查找迭代,说实际的还不好确定,按文档来说是优于LinkedList的。

Deque继承Queue,api如下,主要针对如队列出队列操作。

java.util LinkedList相关类解读_第2张图片
api

不同颜色操作不一样,详情后面文章会进行介绍。

  1. 内部实现原理
    内部使用Node对象实现,数据保存first和last对象:
java.util LinkedList相关类解读_第3张图片
实现
private static class Node {
        E item;
        Node next;
        Node prev;

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

就是使用prev和next保存数据的之间的关系,内存不需要连续。学过数据结构的应该会熟悉。

  1. Iterator实现
    Iterator()方法返回listIterator实现。取得具体的遍历对象使用下列方法:


    java.util LinkedList相关类解读_第4张图片
    Paste_Image.png
private class ListItr implements ListIterator {
        private Node lastReturned = null;
        private Node next;
        private int nextIndex;
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index); // 这儿是调用上图中遍历取得的对象。使用next,prev迭代。
            nextIndex = index;
        }

        public boolean hasNext() {
            return nextIndex < size;
        }

        public E next() {
            checkForComodification();
            if (!hasNext())
                throw new NoSuchElementException();

            lastReturned = next;
            next = next.next;
            nextIndex++;
            return lastReturned.item;
        }

        public boolean hasPrevious() {
            return nextIndex > 0;
        }

        public E previous() {
            checkForComodification();
            if (!hasPrevious())
                throw new NoSuchElementException();

            lastReturned = next = (next == null) ? last : next.prev;
            nextIndex--;
            return lastReturned.item;
        }

        public int nextIndex() {
            return nextIndex;
        }

        public int previousIndex() {
            return nextIndex - 1;
        }

        public void remove() {
            checkForComodification();
            if (lastReturned == null)
                throw new IllegalStateException();

            Node lastNext = lastReturned.next;
            unlink(lastReturned);
            if (next == lastReturned)
                next = lastNext;
            else
                nextIndex--;
            lastReturned = null;
            expectedModCount++;
        }

        public void set(E e) {
            if (lastReturned == null)
                throw new IllegalStateException();
            checkForComodification();
            lastReturned.item = e;
        }

        public void add(E e) {
            checkForComodification();
            lastReturned = null;
            if (next == null)
                linkLast(e);
            else
                linkBefore(e, next);
            nextIndex++;
            expectedModCount++;
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

好了,基本完成了。

你可能感兴趣的:(java.util LinkedList相关类解读)