Collection学习(二)

来源:互联网 发布:《淘金记》知乎 编辑:程序博客网 时间:2024/04/28 21:06

一、概述

LinkedList源码分析:
public class LinkedList<E>extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable,java.io.Serializable
LinkedList是一个双向链表,继承自AbstractSequentialList<E>,并实现了多个接口

1、构造方法public LinkedList() {}//将集合传入构造方法,转换成链表结构public LinkedList(Collection<? extends E> c) {this();addAll(c);}public boolean addAll(Collection<? extends E> c) {return addAll(size, c);}//将集合转换为链表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;//succ保存当前角标所对应的元素if (index == size) {    succ = null;    pred = last;//当index == size 的时候,当前元素的上一个元素就是之前已存在链表的最后一个元素} else { //node方法的主要作用就是判断index所处位置是在之前链表的上半部分还是下半部分,//在上半部分就从第一个元素开始循环,循环到index位置时返回元素,//如果是在后半部分,那么就从最后一个元素往前循环,循环到index位置时返回元素,这样可以提高效率    succ = node(index);    pred = succ.prev; //得到index所在元素的上一个元素引用}for (Object o : a) {    @SuppressWarnings("unchecked") E e = (E) o;    Node<E> newNode = new Node<>(pred, e, null);    if (pred == null)  //如果前一个元素为null, 那么说明不存在链表,将该元素设置为链表的第一个元素first = newNode;    elsepred.next = newNode; //如果前一个元素存在,那么就从前一个元素的角标处插入    pred = newNode;}if (succ == null) {    last = pred;} else {    pred.next = succ;    succ.prev = pred;}size += numNew;modCount++;return true;}2.存储方式add方法: public void add(int index, E element) {        checkPositionIndex(index);        if (index == size) //当index == size的时候,已存在链表的最后一个元素就是当前待插入元素的上一个节点            linkLast(element);        else            linkBefore(element, node(index));    } 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++;    }  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++;    }remove方法:public E remove(int index) {checkElementIndex(index);return unlink(node(index));//node(index)获取index位置的节点}// 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;    }Set方法://替换该列表中的指定元素public E set(int index, E element) {checkElementIndex(index);Node<E> x = node(index); E oldVal = x.item;x.item = element;return oldVal;}get方法:public E get(int index) {checkElementIndex(index);return node(index).item;}

二、LinkedList和ArrayList

1、ArrayList基于数组实现,因此具有: 有序、元素可重复、插入慢、 索引快 这些数组的特性; 
2、LinkedList 基于双向链表实现, 因此具有链表 插入快、 索引慢的特性;
3.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。 
4.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。

0 0
原创粉丝点击