简介

LinkedList实现了List接口和Deque接口,所以LinkedList既可以作为链表也可以作为双端队列。

另外LinkedList是线程不安全的,如果想要使LinkedList变为线程安全,可调用工具类Collections类中的synchronizedList方法

  1. List LinkedList = Collections.synchronizedList(new LinkedList())

基本的结构与构造方法

Node

  1. private static class Node<E> {
  2. E item;//节点值
  3. Node<E> next;//后继节点
  4. Node<E> prev;//前驱节点
  5. Node(Node<E> prev, E element, Node<E> next) {
  6. this.item = element;
  7. this.next = next;
  8. this.prev = prev;
  9. }
  10. }

构造方法

  1. //空参构造
  2. public LinkedList(){
  3. }
  4. //通过已有集合创建链表的构造方法
  5. public LinkedList(Collection<? extends E> c){
  6. this();
  7. addAll(c);
  8. }

add方法

add(E e)将元素添加到链表尾部

  1. public boolean add(E e){
  2. linkLast(e);
  3. return true;
  4. }
  5. void linkLast(E e){
  6. final Node<E> l = last;
  7. final Node<E> newNode = new Node<>(l,e,null);
  8. last= newNode;
  9. //当链表不存在node的情况
  10. if(l == null)
  11. first = newNode;
  12. else
  13. l.next = newNode;
  14. size++;
  15. modCount++;
  16. }

add(int index , E e)将元素添加到指定的位置

  1. public void add(int index, E element) {
  2. checkPositionIndex(index); //检查索引是否处于[0-size]之间
  3. if (index == size)//添加在链表尾部
  4. linkLast(element);
  5. else//添加在链表中间
  6. linkBefore(element, node(index));
  7. }

addAll方法

addAll(Collection c):将集合插入到链表尾部

  1. public boolean addAll(Collection<? extends E> c) {
  2. return addAll(size, c);
  3. }

addAll(int index,Collection c):将集合从指定位置开始插入

  1. public boolean addAll(int index, Collection<? extends E> c) {
  2. //1:检查index范围是否在size之内
  3. checkPositionIndex(index);
  4. //2:toArray()方法把集合的数据存到对象数组中
  5. Object[] a = c.toArray();
  6. int numNew = a.length;
  7. if (numNew == 0)
  8. return false;
  9. //3:得到插入位置的前驱节点和后继节点
  10. Node<E> pred, succ;
  11. //如果插入位置为尾部,前驱节点为last,后继节点为null
  12. if (index == size) {
  13. succ = null;
  14. pred = last;
  15. }
  16. //否则,调用node()方法得到后继节点,再得到前驱节点
  17. else {
  18. succ = node(index);
  19. pred = succ.prev;
  20. }
  21. // 4:遍历数据将数据插入
  22. for (Object o : a) {
  23. @SuppressWarnings("unchecked") E e = (E) o;
  24. //创建新节点
  25. Node<E> newNode = new Node<>(pred, e, null);
  26. //如果插入位置在链表头部
  27. if (pred == null)
  28. first = newNode;
  29. else
  30. pred.next = newNode;
  31. pred = newNode;
  32. }
  33. //如果插入位置在尾部,重置last节点
  34. if (succ == null) {
  35. last = pred;
  36. }
  37. //否则,将插入的链表与先前链表连接起来
  38. else {
  39. pred.next = succ;
  40. succ.prev = pred;
  41. }
  42. size += numNew;
  43. modCount++;
  44. return true;
  45. }

总结起来共分为几步:

  1. 检查index范围是否超过size
  2. 将待加入的集合转为array
  3. 获得index插入位置的前驱节点prev和后继节点succ
  4. 遍历待加入的array并通过prev节点插入至链表
  5. 连接插入后的prev和succ

获取头结点的方法

  1. public E getFirst() {
  2. final Node<E> f = first;
  3. if (f == null)
  4. throw new NoSuchElementException();
  5. return f.item;
  6. }
  7. public E element() {
  8. return getFirst();
  9. }
  10. public E peek() {
  11. final Node<E> f = first;
  12. return (f == null) ? null : f.item;
  13. }
  14. public E peekFirst() {
  15. final Node<E> f = first;
  16. return (f == null) ? null : f.item;
  17. }

getFirst()、element()和peek()、peekFirst()的区别在于对链表为空的处理,是抛出异常还是返回null。

根据对象获得索引

有可能是因为==运算符的速度较o.equals快,所以要分为对象是否为null的两种情况

  1. public int indexOf(Object o) {
  2. int index = 0;
  3. if (o == null) {
  4. //从头遍历
  5. for (Node<E> x = first; x != null; x = x.next) {
  6. if (x.item == null)
  7. return index;
  8. index++;
  9. }
  10. } else {
  11. //从头遍历
  12. for (Node<E> x = first; x != null; x = x.next) {
  13. if (o.equals(x.item))
  14. return index;
  15. index++;
  16. }
  17. }
  18. return -1;
  19. }
  20. public int lastIndexOf(Object o) {
  21. int index = size;
  22. if (o == null) {
  23. //从尾遍历
  24. for (Node<E> x = last; x != null; x = x.prev) {
  25. index--;
  26. if (x.item == null)
  27. return index;
  28. }
  29. } else {
  30. //从尾遍历
  31. for (Node<E> x = last; x != null; x = x.prev) {
  32. index--;
  33. if (o.equals(x.item))
  34. return index;
  35. }
  36. }
  37. return -1;
  38. }