分析Java中ArrayList與LinkedList列表結(jié)構(gòu)的源碼
一、ArrayList源碼分析(JDK7)
ArrayList內(nèi)部維護(hù)了一個(gè)動(dòng)態(tài)的Object數(shù)組,ArrayList的動(dòng)態(tài)增刪就是對(duì)這個(gè)對(duì)組的動(dòng)態(tài)的增加和刪除。
1、ArrayList構(gòu)造以及初始化
ArrayList實(shí)例變量 //ArrayList默認(rèn)容量 private static final int DEFAULT_CAPACITY = 10; //默認(rèn)空的Object數(shù)組, 用于定義空的ArrayList private static final Object[] EMPTY_ELEMENTDATA = {}; //ArrayList存放存放元素的Object數(shù)組 private transient Object[] elementData; //ArrayList中元素的數(shù)量 private int size;
ArrayList構(gòu)造函數(shù):
無(wú)參構(gòu)造函數(shù): 即構(gòu)造一個(gè)空的Object[]
public ArrayList() { super(); this.elementData = EMPTY_ELEMENTDATA; }
指定容量大小構(gòu)造:
public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; }
指定某一實(shí)現(xiàn)Collection接口的集合構(gòu)造:
public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); }
這里也說(shuō)明了Collection的作用, java-collection-framwork設(shè)計(jì)Collection接口而不是直接使用List,Set等接口的原因。
2、ArrayList的容量分配機(jī)制
ArrayList的容量上限: ArrayList容量是有上限的,理論允許分配Integer.Max_VALUE - 8大小的容量。但是能分配多少還跟堆棧設(shè)置有關(guān), 需要設(shè)置VM參數(shù)
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
調(diào)用Add方法時(shí)擴(kuò)容規(guī)則
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
ensureCapacityInternal(int)方法實(shí)際上確定一個(gè)最小擴(kuò)容大小。
private void ensureCapacityInternal(int minCapacity) { if (elementData == EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }
關(guān)于modCount: modCount定義在抽象類(lèi)AbstratList中, 源碼的注釋基本說(shuō)明了它的用處:在使用迭代器遍歷的時(shí)候,用來(lái)檢查列表中的元素是否發(fā)生結(jié)構(gòu)性變化(列表元素?cái)?shù)量發(fā)生改變的一個(gè)計(jì)數(shù))了,主要在多線程環(huán)境下需要使用,防止一個(gè)線程正在迭代遍歷,另一個(gè)線程修改了這個(gè)列表的結(jié)構(gòu)。
grow方法為真正的擴(kuò)容方法
private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
其中對(duì)大容量擴(kuò)容多少還有個(gè)hugeCapacity方法
private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
總結(jié):
每次擴(kuò)容都會(huì)伴隨著數(shù)組的復(fù)制操作, 因此一次給定恰當(dāng)?shù)娜萘繒?huì)提高一點(diǎn)性能。
下圖是我歸納的整個(gè)擴(kuò)容流程:
3.ArrayList迭代器
ArrayList的迭代器主要有兩種Itr和ListItr, 但是在jDK1.8中還添加了一個(gè)ArrayListSpliterator, 下面分別學(xué)一下Itr和ListItr的源碼分析。
(1)Itr:只能向后遍歷
private class Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such //expectedModCount 是modCount的一個(gè)副本 int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); //記錄當(dāng)前位置 int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); //下一個(gè)元素的位置 cursor = i + 1; return (E) elementData[lastRet = i]; } //使用迭代器的remove方法 public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { //注意內(nèi)部類(lèi)調(diào)用外部類(lèi)的方式 ArrayList.this.remove(lastRet); //remove之后需要重新調(diào)整各個(gè)指針的位置 cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
從源碼中可以看出Itr迭代器是向前迭代器, 它提供了一個(gè)next方法用于獲取ArrayList中的元素。
checkForComodification是java-collection-framwork中的一種fail-fast的錯(cuò)誤檢測(cè)機(jī)制。在多線程環(huán)境下對(duì)同一個(gè)集合操作,就可能觸發(fā)fail-fast機(jī)制, 拋出ConcurrentModificationException異常。
Itr迭代器定義了一個(gè)expectedModCount記錄modCount副本。在ArrayList執(zhí)行改變結(jié)構(gòu)的操作的時(shí)候例如Add, remove, clear方法時(shí)modCount的值會(huì)改變。
通過(guò)Itr源碼可以看出調(diào)用next和remove方法會(huì)觸發(fā)fail-fast檢查。此時(shí)如果在遍歷該集合時(shí), 存在其他線程正在執(zhí)行改變?cè)摷辖Y(jié)構(gòu)的操作時(shí)就會(huì)發(fā)生異常。
(2)ListItr:支持向前和向后遍歷,下面看看ListItr的源碼:
private class ListItr extends Itr implements ListIterator<E> { ListItr(int index) { super(); cursor = index; } public boolean hasPrevious() { return cursor != 0; } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } @SuppressWarnings("unchecked") public E previous() { checkForComodification(); //arrayList前一個(gè)元素的位置 int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; } //該迭代器中添加了set方法 public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } //該迭代器添加了add方法 public void add(E e) { checkForComodification(); try { int i = cursor; ArrayList.this.add(i, e); //重新標(biāo)記指針位置 cursor = i + 1; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } }
ListItr的實(shí)現(xiàn)基本與Itr一致, 添加了可以先前遍歷的方法以及add與set方法。
(3)使用java.util.concurrent中的CopyOnWriteArrayList解決fast-fail問(wèn)題
CopyOnWriteArrayList是線程安全的, 具體看一下它的add方法源碼:
public boolean add(E e) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len + 1); newElements[len] = e; setArray(newElements); return true; } finally { lock.unlock(); } }
CopyOnWriteArrayList就是寫(xiě)時(shí)復(fù)制的ArrayList。當(dāng)開(kāi)始寫(xiě)數(shù)據(jù)的操作時(shí)候, Arrays.copyOf一個(gè)新的數(shù)組, 這樣不會(huì)影響讀操作。
這樣的代價(jià)就是會(huì)損耗內(nèi)存, 帶來(lái)性能的問(wèn)題。CopyOnWriteArrayList寫(xiě)的時(shí)候在內(nèi)存中生成一個(gè)副本對(duì)象, 同時(shí)原來(lái)的對(duì)象仍然存在。
CopyOnWriteArrayList無(wú)法保證數(shù)據(jù)實(shí)時(shí)的一致, 只能保證結(jié)果的一致。適用于并發(fā)下讀多寫(xiě)少得場(chǎng)景, 例如緩存。
(4)ArrayList的其他方法源碼:
一個(gè)私有方法batchRemove(Collection<?>c, boolean complement), 即批量移除操作
private boolean batchRemove(Collection<?> c, boolean complement) { //下面會(huì)提到使用final的原因 final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { //遍歷List中的元素,進(jìn)行驗(yàn)證 for (; r < size; r++) if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { //try中如果出現(xiàn)異常,則保證數(shù)據(jù)一致性執(zhí)行下面的copy操作 if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } //清理無(wú)用的元素, 通知GC回收 if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; }
final修飾的變量指的是同一個(gè)引用, 為了后面保持?jǐn)?shù)據(jù)的一致性。
此方法,想保留Collection c中的元素時(shí), complement值為true; 想移除c中的元素時(shí), complement值為false。這樣就分別變成了retainAll和removeAll方法。
swap:交換arrayList中的某兩個(gè)位置的
二、LinkedList源碼分析(JDK7)
LinkedList即鏈表, 相對(duì)于順序表, 鏈表存儲(chǔ)數(shù)據(jù)不需要使用地址連續(xù)的內(nèi)存單元。減少了修改容器結(jié)構(gòu)而帶來(lái)的移動(dòng)元素的問(wèn)題,順序訪問(wèn)相對(duì)高效。
1、結(jié)點(diǎn)(Node)的定義
JDK中的LinkedList是雙向鏈表, 每個(gè)結(jié)點(diǎn)分別存有上一個(gè)結(jié)點(diǎn)和下一個(gè)結(jié)點(diǎn)的信息。它的定義如下:
private static class Node<E> { E item; Node<E> next; Node<E> prev; Node<E> (Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
2、LinkedList構(gòu)造以及初始化
成員: LinkedList中維護(hù)了3個(gè)成員變量, 用以記錄鏈表中結(jié)點(diǎn)的個(gè)數(shù), 結(jié)點(diǎn)的前驅(qū)以及后繼
transient int size = 0; transient Node<E> first; transient Node<E> last;
構(gòu)造函數(shù): 默認(rèn)構(gòu)造函數(shù)即構(gòu)造一個(gè)空的LinkedList
public LinkedList() {}
或者根據(jù)其他容器進(jìn)行構(gòu)造, 后面我們會(huì)自己寫(xiě)一個(gè)構(gòu)造一個(gè)有序的鏈表
public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
這里給出一點(diǎn)補(bǔ)充, 關(guān)于泛型修飾符? super T 與 ? extends T的區(qū)別,參見(jiàn)這篇文章泛型中? super T和? extends T的區(qū)別
3、LinkedList的結(jié)構(gòu)操作
頭插法: 即在鏈表頭插入一個(gè)元素
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++; }
尾插法: 即在鏈表尾部插入一個(gè)元素
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++; }
插入到當(dāng)前結(jié)點(diǎn)之前: 找當(dāng)前結(jié)點(diǎn)的前驅(qū)
void linkBefore(E e, Node<E> succ) { //確定當(dāng)然結(jié)點(diǎn)非空 final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; //判斷當(dāng)前結(jié)點(diǎn)是否是第一個(gè)結(jié)點(diǎn) if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
頭刪法: 刪除鏈表的第一個(gè)結(jié)點(diǎn)
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; }
尾刪法:刪除鏈表的最后一個(gè)結(jié)點(diǎn)
private E unlinkLast(Node<E> l) { //保證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; }
4、保持List接口與Deque的一致性
List接口允許使用下標(biāo)來(lái)實(shí)現(xiàn)對(duì)容器的隨機(jī)訪問(wèn),對(duì)于數(shù)組這種實(shí)現(xiàn)隨機(jī)訪問(wèn)是很容易的。對(duì)于鏈表,JDK也從邏輯上利用鏈表中結(jié)點(diǎn)的計(jì)數(shù)給出了隨機(jī)訪問(wèn)的實(shí)現(xiàn)
Node<E> node(int index) { //確保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; } }
index 屬于前半部分的計(jì)數(shù), 從頭遍歷查找。index屬于后半部分的計(jì)數(shù), 從末尾遍歷查找。充分利用雙向鏈表的特點(diǎn)。
因此,add(int index, T t), get(int), set(int)等方法就可以很容易的實(shí)現(xiàn)。
LinkedList實(shí)現(xiàn)了Deque接口, 也就是LinkedList實(shí)現(xiàn)了雙端隊(duì)列容器的方法,下面給出一些API的總結(jié)。
5、LinkedList的遍歷
既然LinkedList是雙向鏈表, 自然就可以前后遍歷。與ArrayList同樣, 涉及到多線程操作容器的時(shí)候LinkedList也會(huì)出現(xiàn)fail-fast問(wèn)題。
對(duì)于fail-fast問(wèn)題上篇已經(jīng)講解過(guò), 這里就不說(shuō)了。
關(guān)于迭代器,LinkedList有l(wèi)istIterator雙向迭代器, 和DescendingIterator逆序迭代器。都很簡(jiǎn)單。源碼不在分析
如果遍歷元素的話(huà), 隨機(jī)訪問(wèn)的代價(jià)是比較大得。
三、LinkedList,ArrayList, Vector總結(jié)
1、LinkedList與ArrayList
ArrayList是實(shí)現(xiàn)了基于動(dòng)態(tài)數(shù)組的數(shù)據(jù)結(jié)構(gòu),LinkedList基于鏈表的數(shù)據(jù)結(jié)構(gòu)。
對(duì)于隨機(jī)訪問(wèn)get和set,ArrayList覺(jué)得優(yōu)于LinkedList,因?yàn)長(zhǎng)inkedList要移動(dòng)指針。
對(duì)于新增和刪除操作add和remove,LinedList比較占優(yōu)勢(shì),因?yàn)锳rrayList要移動(dòng)數(shù)據(jù)。這一點(diǎn)要看實(shí)際情況的。若只對(duì)單條數(shù)據(jù)插入或刪除,ArrayList的速度反而優(yōu)于LinkedList。但若是批量隨機(jī)的插入刪除數(shù)據(jù),LinkedList的速度大大優(yōu)于ArrayList. 因?yàn)锳rrayList每插入一條數(shù)據(jù),要移動(dòng)插入點(diǎn)及之后的所有數(shù)據(jù)。
2、ArrayList與Vector
vector是線程同步的,所以它也是線程安全的,而arraylist是線程異步的,是不安全的。如果不考慮到線程的安全因素,一般用arraylist效率比較高。
如果集合中的元素的數(shù)目大于目前集合數(shù)組的長(zhǎng)度時(shí),vector增長(zhǎng)率為目前數(shù)組長(zhǎng)度的100%,而arraylist增長(zhǎng)率為目前數(shù)組長(zhǎng)度的50%.如過(guò)在集合中使用數(shù)據(jù)量比較大的數(shù)據(jù),用vector有一定的優(yōu)勢(shì)。
如果查找一個(gè)指定位置的數(shù)據(jù),vector和arraylist使用的時(shí)間是相同的,都是0(1),這個(gè)時(shí)候使用vector和arraylist都可以。而如果移動(dòng)一個(gè)指定位置的數(shù)據(jù)花費(fèi)的時(shí)間為0(n-i)n為總長(zhǎng)度,這個(gè)時(shí)候就應(yīng)該考慮到使用linklist,因?yàn)樗苿?dòng)一個(gè)指定位置的數(shù)據(jù)所花費(fèi)的時(shí)間為0(1),而查詢(xún)一個(gè)指定位置的數(shù)據(jù)時(shí)花費(fèi)的時(shí)間為0(i)。
ArrayList 和Vector是采用數(shù)組方式存儲(chǔ)數(shù)據(jù),此數(shù)組元素?cái)?shù)大于實(shí)際存儲(chǔ)的數(shù)據(jù)以便增加和插入元素,都允許直接序號(hào)索引元素,但是插入數(shù)據(jù)要設(shè)計(jì)到數(shù)組元素移動(dòng)等內(nèi)存操作,所以索引數(shù)據(jù)快插入數(shù)據(jù)慢,Vector由于使用了synchronized方法(線程安全)所以性能上比ArrayList要差,LinkedList使用雙向鏈表實(shí)現(xiàn)存儲(chǔ),按序號(hào)索引數(shù)據(jù)需要進(jìn)行向前或向后遍歷,但是插入數(shù)據(jù)時(shí)只需要記錄本項(xiàng)的前后項(xiàng)即可,所以插入數(shù)度較快!
相關(guān)文章
java request.getHeader("user-agent")獲取瀏覽器信息的方法
這篇文章主要介紹了java request.getHeader("user-agent")獲取瀏覽器信息的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Spring?BeanFactory容器的構(gòu)建和使用示例詳解
BeanFactory是Spring框架中的一部分,它提供了IoC(控制反轉(zhuǎn))的實(shí)現(xiàn)機(jī)制,下面小編就來(lái)和大家簡(jiǎn)單聊聊BeanFactory容器的構(gòu)建和使用示例吧2023-07-07Java 服務(wù)端消息推送的實(shí)現(xiàn)小結(jié)
本文主要介紹了Java 服務(wù)端消息推送的實(shí)現(xiàn)小結(jié),主要包括四種常見(jiàn)的消息實(shí)時(shí)推送方案:短輪詢(xún)、長(zhǎng)輪詢(xún)、SSE?和?WebSocket,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10SpringBoot詳細(xì)列舉常用注解的說(shuō)明
在開(kāi)發(fā)SpringBoot程序的過(guò)程中,有可能與其他業(yè)務(wù)系統(tǒng)進(jìn)行對(duì)接開(kāi)發(fā),獲取封裝公共的API接口等等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot常見(jiàn)的注解的相關(guān)資料,需要的朋友可以參考下2022-06-06Java之System.getProperty()的作用及使用說(shuō)明
這篇文章主要介紹了Java之System.getProperty()的作用及使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04SpringBoot中時(shí)間類(lèi)型 序列化、反序列化、格式處理示例代碼
這篇文章主要介紹了SpringBoot中時(shí)間類(lèi)型 序列化、反序列化、格式處理示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08