一天一个类,一点也不累 之 LinkedList

咱们的口号是,一天一个类,一点也不累 。。java

今天要讲的是---LinkedList安全

首先,仍是看看他的组织结构ide

Class LinkedList<E>

    java.lang.Object
        java.util.AbstractCollection<E>
            java.util.AbstractList<E>
                java.util.AbstractSequentialList<E>
                    java.util.LinkedList<E>

    Type Parameters:
        E - the type of elements held in this collection

    All Implemented Interfaces:
        Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>


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

相比上两次介绍的两个类,他的实现接口多了一个Queue & Deque(一个是队列一个是双端队列)this

既然这个也实现了List,一样具备List的一些性质,不知你们是否还记得List中的对象允许为NULL.spa

在API的官网上面明确说了一句:【Note that this implementation is not synchronized.】 也就是说LinkedList也是非线程安全的。
线程

和ArrayList同样,库中存在线程安全的实现方法:code

  List list = Collections.synchronizedList(new LinkedList(...));对象

因此在非线程安全的容器里面会有一个用来记录该对象改变次数的计数器modCount.
blog

【首先声明一下,这个LinkedList里面维持这两个对象,一个是first,一个是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;
        }
    }

 

一、构造方法:

  LinkedList()

    Constructs an empty list.

  LinkedList(Collection<? extends E> c)

    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 
二、首先特别解释一下, transient,这样的对象在行进序列话的时候,  是不被需序列化的,是临时的。
   由于在读源码的时候常常会出现这个关键字。
 
三、由于这个是链表,因此提供了一些关于链表的操做。
  e.g. getFirst() getLast()。。。。。。
 
四、值得注意的是从1.6开始,新增了一项称道的方法(虽然本身能够方便实现),这就是可以生成倒序的迭代器。
 1 /**
 2      * Adapter to provide descending iterators via ListItr.previous
 3      */
 4     private class DescendingIterator implements Iterator<E> {
 5         private final ListItr itr = new ListItr(size());
 6         public boolean hasNext() {
 7             return itr.hasPrevious();
 8         }
 9         public E next() {
10             return itr.previous();
11         }
12         public void remove() {
13             itr.remove();
14         }
15     }

这样以来就知足了在特定场合的须要。

 

哇喔,这个类要说的好少啊!~~~

再也不说的多少,而在多思多练。

中国有句谚语:

        不闻不若闻之,闻之不若见之,见之不若知之,知之不若行之。

相关文章
相关标签/搜索