Java集合源码分析(二)——LinkedList源码分析

1、LinkedList简介:

  • LinkedList是一种可以在任何位置进行高效插入和移除操作的列表,基于双向链表实现
  • 可以被当做堆栈、队列和双向队列使用
  • 实现了List接口
  • 实现了Cloneable接口
  • 实现了Deque接口
  • 实现了java.io.Serializable接口

2、数据结构:

LinkedList是基于双向链表实现的,内部类Node<E>实现了双向链表数据结构,所以意味着可以从头开始正向遍历,也可以从尾开始逆向遍历

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;
        }
 }

3、继承结构与层次关系

相比于ArrayList,LinkedList多了一个AbstractSequentialList,主要用于抽象处类似于LinkedList这种类的共同方法,即基于链表实现的,顺序存取结构

4、成员变量:

//实际元素个数     
transient int size = 0;
	
//头节点
transient Node<E> first;
	
//尾节点
transient Node<E> last;

5、构造方法:

  •  无参构造方法:空参构造方法
public LinkedList() {
}
  • 有参构造方法:添加指定集合元素到LinkedList,会调用无参构造方法
public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
}

6、核心方法:

add方法(6个)

  • 在表尾添加元素
public boolean add(E e) {
        linkLast(e);
        return true;
}
  •  在指定位置处添加元素
public void add(int index, E element) {
       //检查索引
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
}
  • 在指定位置处添加集合元素
public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) {
            succ = null;
            pred = last;
        } else {
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            //判断是不是表头
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

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

        size += numNew;
        modCount++;
        return true;
}
  • 在表尾添加集合元素
 public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
 }
  • 在表头添加元素
 public void addFirst(E e) {
        linkFirst(e);
 }
  • 在表尾添加元素
public void addLast(E e) {
        linkLast(e);
}

内部调用的方法:

  • linkBefore:在非空节点前添加元素,实现具体添加到非空节点前的逻辑功能
void linkBefore(E e, Node<E> succ) {
        // 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++;
}
  •  linkFirst:在表头插入一个元素,实现具体添加到头部的逻辑功能
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++;
}
  • linkLast:在表尾插入一个元素,实现具体添加到尾部的逻辑功能
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++;
 }

remove方法(7个)

  • 删除并返回第一个元素
    public E remove() {
            return removeFirst();
    }
  • 删除指定索引处的元素
public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
}
  • 删除第一个与指定元素相同的元素
public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
 }
  • 删除最后一个与指定元素相同的元素
public boolean removeLastOccurrence(Object o) {
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
}

调用内部函数:

unlink:具体实现删除元素操作

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;
}

unlinkFirst:具体实现删除头部操作

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;
}

unlinkLast:具体实现删除尾部操作

 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;
}

get方法:根据索引查询元素

 public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
 }

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;
        }
 }

indexOf:根据元素查索引

public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
 }

 7、迭代器

public ListIterator<E> listIterator(int index) {
        checkPositionIndex(index);
        return new ListItr(index);
    }

    private class ListItr implements ListIterator<E> {
        private Node<E> lastReturned;
        private Node<E> next;
        private int nextIndex;
        private int expectedModCount = modCount;

        ListItr(int index) {
            // assert isPositionIndex(index);
            next = (index == size) ? null : node(index);
            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<E> 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++;
        }

        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            while (modCount == expectedModCount && nextIndex < size) {
                action.accept(next.item);
                lastReturned = next;
                next = next.next;
                nextIndex++;
            }
            checkForComodification();
        }

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

问题1:为什么LinkedList使用Iterator性能更好

问题2:什么情况下会抛出ConcurrentModificationException()异常

答:expectedModCount与modCount不相等的时候,也就是在迭代时用List修改集合元素,而不是用迭代器的方法修改集合元素

8、总结

  • LinkedList本质上是一个双向链表,通过一个Node内部类实现
  • 能存储null值
  • 与ArrayList相比,LinkedList在删除和增加操作上性能更好
  • 不存在容量不足的情况
  • LinkedList不仅能向前迭代,还能向后迭代,而且在迭代的过程中还能修改、添加、移除值
  • 能当链表、队列、栈、双端队列使用

猜你喜欢

转载自blog.csdn.net/buer219/article/details/96182606