成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

站在巨人肩膀上看源碼-LinkedList

learn_shifeng / 1330人閱讀

摘要:在閱讀源碼之前,我們先對的整體實現(xiàn)進行大致說明實際上是通過雙向鏈表去實現(xiàn)的。獲取的最后一個元素由于是雙向鏈表而表頭不包含數(shù)據(jù)。實際上是判斷雙向鏈表的當前節(jié)點是否達到開頭反向迭代器獲取下一個元素。

第1部分 LinkedList介紹 LinkedList簡介

LinkedList 是一個繼承于AbstractSequentialList的雙向鏈表。它也可以被當作堆棧、隊列或雙端隊列進行操作。
LinkedList 實現(xiàn) List 接口,能對它進行隊列操作。
LinkedList 實現(xiàn) Deque 接口,即能將LinkedList當作雙端隊列使用。
LinkedList 實現(xiàn)了Cloneable接口,即覆蓋了函數(shù)clone(),能克隆。
LinkedList 實現(xiàn)java.io.Serializable接口,這意味著LinkedList支持序列化,能通過序列化去傳輸。
LinkedList 是非同步的。

LinkedList構(gòu)造函數(shù)
// 默認構(gòu)造函數(shù)
LinkedList()

// 創(chuàng)建一個LinkedList,保護Collection中的全部元素。
LinkedList(Collection collection)
LinkedList的API
LinkedList的API
boolean       add(E object)
void          add(int location, E object)
boolean       addAll(Collection collection)
boolean       addAll(int location, Collection collection)
void          addFirst(E object)
void          addLast(E object)
void          clear()
Object        clone()
boolean       contains(Object object)
Iterator   descendingIterator()
E             element()
E             get(int location)
E             getFirst()
E             getLast()
int           indexOf(Object object)
int           lastIndexOf(Object object)
ListIterator     listIterator(int location)
boolean       offer(E o)
boolean       offerFirst(E e)
boolean       offerLast(E e)
E             peek()
E             peekFirst()
E             peekLast()
E             poll()
E             pollFirst()
E             pollLast()
E             pop()
void          push(E e)
E             remove()
E             remove(int location)
boolean       remove(Object object)
E             removeFirst()
boolean       removeFirstOccurrence(Object o)
E             removeLast()
boolean       removeLastOccurrence(Object o)
E             set(int location, E object)
int           size()
 T[]       toArray(T[] contents)
Object[]     toArray()
AbstractSequentialList簡介

在介紹LinkedList的源碼之前,先介紹一下AbstractSequentialList。畢竟,LinkedList是AbstractSequentialList的子類。

AbstractSequentialList 實現(xiàn)了get(int index)、set(int index, E element)、add(int index, E element) 和 remove(int index)這些函數(shù)。這些接口都是隨機訪問List的,LinkedList是雙向鏈表;既然它繼承于AbstractSequentialList,就相當于已經(jīng)實現(xiàn)了“get(int index)這些接口”。

此外,我們?nèi)粜枰ㄟ^AbstractSequentialList自己實現(xiàn)一個列表,只需要擴展此類,并提供 listIterator() 和 size() 方法的實現(xiàn)即可。若要實現(xiàn)不可修改的列表,則需要實現(xiàn)列表迭代器的 hasNext、next、hasPrevious、previous 和 index 方法即可。
inkedList實際上是通過雙向鏈表去實現(xiàn)的。既然是雙向鏈表,那么它的順序訪問會非常高效,而隨機訪問效率比較低。

第2部分 LinkedList數(shù)據(jù)結(jié)構(gòu) LinkedList的繼承關(guān)系
    java.lang.Object
       ?     java.util.AbstractCollection
             ?     java.util.AbstractList
                   ?     java.util.AbstractSequentialList
                         ?     java.util.LinkedList
    
    public class LinkedList
        extends AbstractSequentialList
        implements List, Deque, Cloneable, java.io.Serializable {}
        

LinkedList與Collection關(guān)系如下圖:
---------------------------
![272345393446232.jpg][1]


LinkedList的本質(zhì)是雙向鏈表。
(01) LinkedList繼承于AbstractSequentialList,并且實現(xiàn)了Dequeue接口。 
(02) LinkedList包含兩個重要的成員:header 和 size。
  header是雙向鏈表的表頭,它是雙向鏈表節(jié)點所對應的類Entry的實例。Entry中包含成員變量: previous, next, element。其中,previous是該節(jié)點的上一個節(jié)點,next是該節(jié)點的下一個節(jié)點,element是該節(jié)點所包含的值。 
  size是雙向鏈表中節(jié)點的個數(shù)。
(03)LinkedList數(shù)據(jù)結(jié)構(gòu);
![616953-20160322214504120-1558870057.png][2]


  [1]: /img/bVbbtxf
  [2]: /img/bVbbtxw
說明:如上圖所示,LinkedList底層使用的雙向鏈表結(jié)構(gòu),有一個頭結(jié)點和一個尾結(jié)點,雙向鏈表意味著我們可以從頭開始正向遍歷,或者是從尾開始逆向遍歷,并且可以針對頭部和尾部進行相應的操作。

第3部分 LinkedList源碼解析

為了更了解LinkedList的原理,下面對LinkedList源碼代碼作出分析。

在閱讀源碼之前,我們先對LinkedList的整體實現(xiàn)進行大致說明:

LinkedList實際上是通過雙向鏈表去實現(xiàn)的。既然是雙向鏈表,那么它的順序訪問會非常高效,而隨機訪問效率比較低。
既然LinkedList是通過雙向鏈表的,但是它也實現(xiàn)了List接口{也就是說,它實現(xiàn)了get(int location)、remove(int location)等“根據(jù)索引值來獲取、刪除節(jié)點的函數(shù)”}。LinkedList是如何實現(xiàn)List的這些接口的,如何將“雙向鏈表和索引值聯(lián)系起來的”?
實際原理非常簡單,它就是通過一個計數(shù)索引值來實現(xiàn)的。例如,當我們調(diào)用get(int location)時,首先會比較“l(fā)ocation”和“雙向鏈表長度的1/2”;若前者大,則從鏈表頭開始往后查找,直到location位置;否則,從鏈表末尾開始先前查找,直到location位置。

這就是“雙線鏈表和索引值聯(lián)系起來”的方法。

好了,接下來開始閱讀源碼(只要理解雙向鏈表,那么LinkedList的源碼很容易理解的)



package java.util;
    
    public class LinkedList
        extends AbstractSequentialList
        implements List, Deque, Cloneable, java.io.Serializable
    {
        // 鏈表的表頭,表頭不包含任何數(shù)據(jù)。Entry是個鏈表類數(shù)據(jù)結(jié)構(gòu)。
        private transient Entry header = new Entry(null, null, null);
    
        // LinkedList中元素個數(shù)
        private transient int size = 0;
    
        // 默認構(gòu)造函數(shù):創(chuàng)建一個空的鏈表
        public LinkedList() {
            header.next = header.previous = header;
        }
    
        // 包含“集合”的構(gòu)造函數(shù):創(chuàng)建一個包含“集合”的LinkedList
        public LinkedList(Collection c) {
            this();
            addAll(c);
        }
    
        // 獲取LinkedList的第一個元素
        public E getFirst() {
            if (size==0)
                throw new NoSuchElementException();
    
            // 鏈表的表頭header中不包含數(shù)據(jù)。
            // 這里返回header所指下一個節(jié)點所包含的數(shù)據(jù)。
            return header.next.element;
        }
    
        // 獲取LinkedList的最后一個元素
        public E getLast()  {
            if (size==0)
                throw new NoSuchElementException();
    
            // 由于LinkedList是雙向鏈表;而表頭header不包含數(shù)據(jù)。
            // 因而,這里返回表頭header的前一個節(jié)點所包含的數(shù)據(jù)。
            return header.previous.element;
        }
    
        // 刪除LinkedList的第一個元素
        public E removeFirst() {
            return remove(header.next);
        }
    
        // 刪除LinkedList的最后一個元素
        public E removeLast() {
            return remove(header.previous);
        }
    
        // 將元素添加到LinkedList的起始位置
        public void addFirst(E e) {
            addBefore(e, header.next);
        }
    
        // 將元素添加到LinkedList的結(jié)束位置
        public void addLast(E e) {
            addBefore(e, header);
        }
    
        // 判斷LinkedList是否包含元素(o)
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
    
        // 返回LinkedList的大小
        public int size() {
            return size;
        }
    
        // 將元素(E)添加到LinkedList中
        public boolean add(E e) {
            // 將節(jié)點(節(jié)點數(shù)據(jù)是e)添加到表頭(header)之前。
            // 即,將節(jié)點添加到雙向鏈表的末端。
            addBefore(e, header);
            return true;
        }
    
        // 從LinkedList中刪除元素(o)
        // 從鏈表開始查找,如存在元素(o)則刪除該元素并返回true;
        // 否則,返回false。
        public boolean remove(Object o) {
            if (o==null) {
                // 若o為null的刪除情況
                for (Entry e = header.next; e != header; e = e.next) {
                    if (e.element==null) {
                        remove(e);
                        return true;
                    }
                }
            } else {
                // 若o不為null的刪除情況
                for (Entry e = header.next; e != header; e = e.next) {
                    if (o.equals(e.element)) {
                        remove(e);
                        return true;
                    }
                }
            }
            return false;
        }
    
        // 將“集合(c)”添加到LinkedList中。
        // 實際上,是從雙向鏈表的末尾開始,將“集合(c)”添加到雙向鏈表中。
        public boolean addAll(Collection c) {
            return addAll(size, c);
        }
    
        // 從雙向鏈表的index開始,將“集合(c)”添加到雙向鏈表中。
        public boolean addAll(int index, Collection c) {
            if (index < 0 || index > size)
                throw new IndexOutOfBoundsException("Index: "+index+
                                                    ", Size: "+size);
            Object[] a = c.toArray();
            // 獲取集合的長度
            int numNew = a.length;
            if (numNew==0)
                return false;
            modCount++;
    
            // 設置“當前要插入節(jié)點的后一個節(jié)點”
            Entry successor = (index==size ? header : entry(index));
            // 設置“當前要插入節(jié)點的前一個節(jié)點”
            Entry predecessor = successor.previous;
            // 將集合(c)全部插入雙向鏈表中
            for (int i=0; i e = new Entry((E)a[i], successor, predecessor);
                predecessor.next = e;
                predecessor = e;
            }
            successor.previous = predecessor;
    
            // 調(diào)整LinkedList的實際大小
            size += numNew;
            return true;
        }
    
        // 清空雙向鏈表
        public void clear() {
            Entry e = header.next;
            // 從表頭開始,逐個向后遍歷;對遍歷到的節(jié)點執(zhí)行一下操作:
            // (01) 設置前一個節(jié)點為null 
            // (02) 設置當前節(jié)點的內(nèi)容為null 
            // (03) 設置后一個節(jié)點為“新的當前節(jié)點”
            while (e != header) {
                Entry next = e.next;
                e.next = e.previous = null;
                e.element = null;
                e = next;
            }
            header.next = header.previous = header;
            // 設置大小為0
            size = 0;
            modCount++;
        }
    
        // 返回LinkedList指定位置的元素
        public E get(int index) {
            return entry(index).element;
        }
    
        // 設置index位置對應的節(jié)點的值為element
        public E set(int index, E element) {
            Entry e = entry(index);
            E oldVal = e.element;
            e.element = element;
            return oldVal;
        }
     
        // 在index前添加節(jié)點,且節(jié)點的值為element
        public void add(int index, E element) {
            addBefore(element, (index==size ? header : entry(index)));
        }
    
        // 刪除index位置的節(jié)點
        public E remove(int index) {
            return remove(entry(index));
        }
    
        // 獲取雙向鏈表中指定位置的節(jié)點
        private Entry entry(int index) {
            if (index < 0 || index >= size)
                throw new IndexOutOfBoundsException("Index: "+index+
                                                    ", Size: "+size);
            Entry e = header;
            // 獲取index處的節(jié)點。
            // 若index < 雙向鏈表長度的1/2,則從前先后查找;
            // 否則,從后向前查找。
            if (index < (size >> 1)) {
                for (int i = 0; i <= index; i++)
                    e = e.next;
            } else {
                for (int i = size; i > index; i--)
                    e = e.previous;
            }
            return e;
        }
    
        // 從前向后查找,返回“值為對象(o)的節(jié)點對應的索引”
        // 不存在就返回-1
        public int indexOf(Object o) {
            int index = 0;
            if (o==null) {
                for (Entry e = header.next; e != header; e = e.next) {
                    if (e.element==null)
                        return index;
                    index++;
                }
            } else {
                for (Entry e = header.next; e != header; e = e.next) {
                    if (o.equals(e.element))
                        return index;
                    index++;
                }
            }
            return -1;
        }
    
        // 從后向前查找,返回“值為對象(o)的節(jié)點對應的索引”
        // 不存在就返回-1
        public int lastIndexOf(Object o) {
            int index = size;
            if (o==null) {
                for (Entry e = header.previous; e != header; e = e.previous) {
                    index--;
                    if (e.element==null)
                        return index;
                }
            } else {
                for (Entry e = header.previous; e != header; e = e.previous) {
                    index--;
                    if (o.equals(e.element))
                        return index;
                }
            }
            return -1;
        }
    
        // 返回第一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E peek() {
            if (size==0)
                return null;
            return getFirst();
        }
    
        // 返回第一個節(jié)點
        // 若LinkedList的大小為0,則拋出異常
        public E element() {
            return getFirst();
        }
    
        // 刪除并返回第一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E poll() {
            if (size==0)
                return null;
            return removeFirst();
        }
    
        // 將e添加雙向鏈表末尾
        public boolean offer(E e) {
            return add(e);
        }
    
        // 將e添加雙向鏈表開頭
        public boolean offerFirst(E e) {
            addFirst(e);
            return true;
        }
    
        // 將e添加雙向鏈表末尾
        public boolean offerLast(E e) {
            addLast(e);
            return true;
        }
    
        // 返回第一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E peekFirst() {
            if (size==0)
                return null;
            return getFirst();
        }
    
        // 返回最后一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E peekLast() {
            if (size==0)
                return null;
            return getLast();
        }
    
        // 刪除并返回第一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E pollFirst() {
            if (size==0)
                return null;
            return removeFirst();
        }
    
        // 刪除并返回最后一個節(jié)點
        // 若LinkedList的大小為0,則返回null
        public E pollLast() {
            if (size==0)
                return null;
            return removeLast();
        }
    
        // 將e插入到雙向鏈表開頭
        public void push(E e) {
            addFirst(e);
        }
    
        // 刪除并返回第一個節(jié)點
        public E pop() {
            return removeFirst();
        }
    
        // 從LinkedList開始向后查找,刪除第一個值為元素(o)的節(jié)點
        // 從鏈表開始查找,如存在節(jié)點的值為元素(o)的節(jié)點,則刪除該節(jié)點
        public boolean removeFirstOccurrence(Object o) {
            return remove(o);
        }
    
        // 從LinkedList末尾向前查找,刪除第一個值為元素(o)的節(jié)點
        // 從鏈表開始查找,如存在節(jié)點的值為元素(o)的節(jié)點,則刪除該節(jié)點
        public boolean removeLastOccurrence(Object o) {
            if (o==null) {
                for (Entry e = header.previous; e != header; e = e.previous) {
                    if (e.element==null) {
                        remove(e);
                        return true;
                    }
                }
            } else {
                for (Entry e = header.previous; e != header; e = e.previous) {
                    if (o.equals(e.element)) {
                        remove(e);
                        return true;
                    }
                }
            }
            return false;
        }
    
        // 返回“index到末尾的全部節(jié)點”對應的ListIterator對象(List迭代器)
        public ListIterator listIterator(int index) {
            return new ListItr(index);
        }
    
        // List迭代器
        private class ListItr implements ListIterator {
            // 上一次返回的節(jié)點
            private Entry lastReturned = header;
            // 下一個節(jié)點
            private Entry next;
            // 下一個節(jié)點對應的索引值
            private int nextIndex;
            // 期望的改變計數(shù)。用來實現(xiàn)fail-fast機制。
            private int expectedModCount = modCount;
    
            // 構(gòu)造函數(shù)。
            // 從index位置開始進行迭代
            ListItr(int index) {
                // index的有效性處理
                if (index < 0 || index > size)
                    throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);
                // 若 “index 小于 ‘雙向鏈表長度的一半’”,則從第一個元素開始往后查找;
                // 否則,從最后一個元素往前查找。
                if (index < (size >> 1)) {
                    next = header.next;
                    for (nextIndex=0; nextIndexindex; nextIndex--)
                        next = next.previous;
                }
            }
    
            // 是否存在下一個元素
            public boolean hasNext() {
                // 通過元素索引是否等于“雙向鏈表大小”來判斷是否達到最后。
                return nextIndex != size;
            }
    
            // 獲取下一個元素
            public E next() {
                checkForComodification();
                if (nextIndex == size)
                    throw new NoSuchElementException();
    
                lastReturned = next;
                // next指向鏈表的下一個元素
                next = next.next;
                nextIndex++;
                return lastReturned.element;
            }
    
            // 是否存在上一個元素
            public boolean hasPrevious() {
                // 通過元素索引是否等于0,來判斷是否達到開頭。
                return nextIndex != 0;
            }
    
            // 獲取上一個元素
            public E previous() {
                if (nextIndex == 0)
                throw new NoSuchElementException();
    
                // next指向鏈表的上一個元素
                lastReturned = next = next.previous;
                nextIndex--;
                checkForComodification();
                return lastReturned.element;
            }
    
            // 獲取下一個元素的索引
            public int nextIndex() {
                return nextIndex;
            }
    
            // 獲取上一個元素的索引
            public int previousIndex() {
                return nextIndex-1;
            }
    
            // 刪除當前元素。
            // 刪除雙向鏈表中的當前節(jié)點
            public void remove() {
                checkForComodification();
                Entry lastNext = lastReturned.next;
                try {
                    LinkedList.this.remove(lastReturned);
                } catch (NoSuchElementException e) {
                    throw new IllegalStateException();
                }
                if (next==lastReturned)
                    next = lastNext;
                else
                    nextIndex--;
                lastReturned = header;
                expectedModCount++;
            }
    
            // 設置當前節(jié)點為e
            public void set(E e) {
                if (lastReturned == header)
                    throw new IllegalStateException();
                checkForComodification();
                lastReturned.element = e;
            }
    
            // 將e添加到當前節(jié)點的前面
            public void add(E e) {
                checkForComodification();
                lastReturned = header;
                addBefore(e, next);
                nextIndex++;
                expectedModCount++;
            }
    
            // 判斷 “modCount和expectedModCount是否相等”,依次來實現(xiàn)fail-fast機制。
            final void checkForComodification() {
                if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            }
        }
    
        // 雙向鏈表的節(jié)點所對應的數(shù)據(jù)結(jié)構(gòu)。
        // 包含3部分:上一節(jié)點,下一節(jié)點,當前節(jié)點值。
        private static class Entry {
            // 當前節(jié)點所包含的值
            E element;
            // 下一個節(jié)點
            Entry next;
            // 上一個節(jié)點
            Entry previous;
    
            /**
             * 鏈表節(jié)點的構(gòu)造函數(shù)。
             * 參數(shù)說明:
             *   element  —— 節(jié)點所包含的數(shù)據(jù)
             *   next      —— 下一個節(jié)點
             *   previous —— 上一個節(jié)點
             */
            Entry(E element, Entry next, Entry previous) {
                this.element = element;
                this.next = next;
                this.previous = previous;
            }
        }
    
        // 將節(jié)點(節(jié)點數(shù)據(jù)是e)添加到entry節(jié)點之前。
        private Entry addBefore(E e, Entry entry) {
            // 新建節(jié)點newEntry,將newEntry插入到節(jié)點e之前;并且設置newEntry的數(shù)據(jù)是e
            Entry newEntry = new Entry(e, entry, entry.previous);
            newEntry.previous.next = newEntry;
            newEntry.next.previous = newEntry;
            // 修改LinkedList大小
            size++;
            // 修改LinkedList的修改統(tǒng)計數(shù):用來實現(xiàn)fail-fast機制。
            modCount++;
            return newEntry;
        }
    
        // 將節(jié)點從鏈表中刪除
        private E remove(Entry e) {
            if (e == header)
                throw new NoSuchElementException();
    
            E result = e.element;
            e.previous.next = e.next;
            e.next.previous = e.previous;
            e.next = e.previous = null;
            e.element = null;
            size--;
            modCount++;
            return result;
        }
    
        // 反向迭代器
        public Iterator descendingIterator() {
            return new DescendingIterator();
        }
    
        // 反向迭代器實現(xiàn)類。
        private class DescendingIterator implements Iterator {
            final ListItr itr = new ListItr(size());
            // 反向迭代器是否下一個元素。
            // 實際上是判斷雙向鏈表的當前節(jié)點是否達到開頭
            public boolean hasNext() {
                return itr.hasPrevious();
            }
            // 反向迭代器獲取下一個元素。
            // 實際上是獲取雙向鏈表的前一個節(jié)點
            public E next() {
                return itr.previous();
            }
            // 刪除當前節(jié)點
            public void remove() {
                itr.remove();
            }
        }
    
    
        // 返回LinkedList的Object[]數(shù)組
        public Object[] toArray() {
        // 新建Object[]數(shù)組
        Object[] result = new Object[size];
            int i = 0;
            // 將鏈表中所有節(jié)點的數(shù)據(jù)都添加到Object[]數(shù)組中
            for (Entry e = header.next; e != header; e = e.next)
                result[i++] = e.element;
        return result;
        }
    
        // 返回LinkedList的模板數(shù)組。所謂模板數(shù)組,即可以將T設為任意的數(shù)據(jù)類型
        public  T[] toArray(T[] a) {
            // 若數(shù)組a的大小 < LinkedList的元素個數(shù)(意味著數(shù)組a不能容納LinkedList中全部元素)
            // 則新建一個T[]數(shù)組,T[]的大小為LinkedList大小,并將該T[]賦值給a。
            if (a.length < size)
                a = (T[])java.lang.reflect.Array.newInstance(
                                    a.getClass().getComponentType(), size);
            // 將鏈表中所有節(jié)點的數(shù)據(jù)都添加到數(shù)組a中
            int i = 0;
            Object[] result = a;
            for (Entry e = header.next; e != header; e = e.next)
                result[i++] = e.element;
    
            if (a.length > size)
                a[size] = null;
    
            return a;
        }
    
    
        // 克隆函數(shù)。返回LinkedList的克隆對象。
        public Object clone() {
            LinkedList clone = null;
            // 克隆一個LinkedList克隆對象
            try {
                clone = (LinkedList) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new InternalError();
            }
    
            // 新建LinkedList表頭節(jié)點
            clone.header = new Entry(null, null, null);
            clone.header.next = clone.header.previous = clone.header;
            clone.size = 0;
            clone.modCount = 0;
    
            // 將鏈表中所有節(jié)點的數(shù)據(jù)都添加到克隆對象中
            for (Entry e = header.next; e != header; e = e.next)
                clone.add(e.element);
    
            return clone;
        }
    
        // java.io.Serializable的寫入函數(shù)
        // 將LinkedList的“容量,所有的元素值”都寫入到輸出流中
        private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
            // Write out any hidden serialization magic
            s.defaultWriteObject();
    
            // 寫入“容量”
            s.writeInt(size);
    
            // 將鏈表中所有節(jié)點的數(shù)據(jù)都寫入到輸出流中
            for (Entry e = header.next; e != header; e = e.next)
                s.writeObject(e.element);
        }
    
        // java.io.Serializable的讀取函數(shù):根據(jù)寫入方式反向讀出
        // 先將LinkedList的“容量”讀出,然后將“所有的元素值”讀出
        private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
            // Read in any hidden serialization magic
            s.defaultReadObject();
    
            // 從輸入流中讀取“容量”
            int size = s.readInt();
    
            // 新建鏈表表頭節(jié)點
            header = new Entry(null, null, null);
            header.next = header.previous = header;
    
            // 從輸入流中將“所有的元素值”并逐個添加到鏈表中
            for (int i=0; i

總結(jié):
(01) LinkedList 實際上是通過雙向鏈表去實現(xiàn)的。它包含一個非常重要的內(nèi)部類:Entry。Entry是雙向鏈表節(jié)點所對應的數(shù)據(jù)結(jié)構(gòu),它包括的屬性有:當前節(jié)點所包含的值,上一個節(jié)點,下一個節(jié)點。
(02) 從LinkedList的實現(xiàn)方式中可以發(fā)現(xiàn),它不存在LinkedList容量不足的問題。
(03) LinkedList的克隆函數(shù),即是將全部元素克隆到一個新的LinkedList對象中。
(04) LinkedList實現(xiàn)java.io.Serializable。當寫入到輸出流時,先寫入“容量”,再依次寫入“每一個節(jié)點保護的值”;當讀出輸入流時,先讀取“容量”,再依次讀取“每一個元素”。
(05) 由于LinkedList實現(xiàn)了Deque,而Deque接口定義了在雙端隊列兩端訪問元素的方法。提供插入、移除和檢查元素的方法。每種方法都存在兩種形式:一種形式在操作失敗時拋出異常,另一種形式返回一個特殊值(null 或 false,具體取決于操作)。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69564.html

相關(guān)文章

  • 站在巨人肩膀上看源碼-ArrayList

    摘要:源碼剖析的源碼如下加入了比較詳細的注釋序列版本號基于該數(shù)組實現(xiàn),用該數(shù)組保存數(shù)據(jù)中實際數(shù)據(jù)的數(shù)量帶容量大小的構(gòu)造函數(shù)。該方法被標記了,調(diào)用了系統(tǒng)的代碼,在中是看不到的,但在中可以看到其源碼。 ArrayList簡介 ArrayList是基于數(shù)組實現(xiàn)的,是一個動態(tài)數(shù)組,其容量能自動增長,類似于C語言中的動態(tài)申請內(nèi)存,動態(tài)增長內(nèi)存。ArrayList不是線程安全的,只能用在單線程環(huán)境下,多...

    ThinkSNS 評論0 收藏0
  • 站在巨人肩膀上看源碼-HashSet

    摘要:實際運行上面程序?qū)⒖吹匠绦蜉敵?,這是因為判斷兩個對象相等的標準除了要求通過方法比較返回之外,還要求兩個對象的返回值相等。通常來說,所有參與計算返回值的關(guān)鍵屬性,都應該用于作為比較的標準。 1.HashSet概述:   HashSet實現(xiàn)Set接口,由哈希表(實際上是一個HashMap實例)支持。它不保證set 的迭代順序;特別是它不保證該順序恒久不變。此類允許使用null元素。Hash...

    DevTTL 評論0 收藏0
  • 站在巨人肩膀上看源碼-Map

    摘要:在學習的實現(xiàn)類是基于實現(xiàn)的前,先來介紹下接口及其下的子接口先看下的架構(gòu)圖如上圖是映射接口,中存儲的內(nèi)容是鍵值對。是繼承于的接口。中的內(nèi)容是排序的鍵值對,排序的方法是通過比較器。 Map 在學習Set(Set的實現(xiàn)類是基于Map實現(xiàn)的)、HashMap、TreeMap前,先來介紹下Map接口及其下的子接口.先看下Map的架構(gòu)圖:showImg(https://segmentfault.c...

    xiaotianyi 評論0 收藏0
  • 站在巨人肩膀上看源碼-HashMap(基于jdk1.8)

    摘要:而中,采用數(shù)組鏈表紅黑樹實現(xiàn),當鏈表長度超過閾值時,將鏈表轉(zhuǎn)換為紅黑樹,這樣大大減少了查找時間。到了,當同一個值的節(jié)點數(shù)不小于時,不再采用單鏈表形式存儲,而是采用紅黑樹,如下圖所示。 一. HashMap概述 在JDK1.8之前,HashMap采用數(shù)組+鏈表實現(xiàn),即使用鏈表處理沖突,同一hash值的節(jié)點都存儲在一個鏈表里。但是當位于一個桶中的元素較多,即hash值相等的元素較多時,通過...

    劉玉平 評論0 收藏0
  • 站在巨人肩膀上看源碼-ConcurrentHashMap

    摘要:一出現(xiàn)背景線程不安全的因為多線程環(huán)境下,使用進行操作會引起死循環(huán),導致利用率接近,所以在并發(fā)情況下不能使用。是由數(shù)組結(jié)構(gòu)和數(shù)組結(jié)構(gòu)組成。用來表示需要進行的界限值。也是,這使得能夠讀取到最新的值而不需要同步。 一、出現(xiàn)背景 1、線程不安全的HashMap 因為多線程環(huán)境下,使用Hashmap進行put操作會引起死循環(huán),導致CPU利用率接近100%,所以在并發(fā)情況下不能使用HashMap。...

    n7then 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<