Java容器類原始碼-Vector的最全的原始碼分析(四)
(31) public synchronized boolean retainAll(Collection<?> c)
原始碼解釋:
將陣列中不是c中包含的元素全部移除。呼叫AbstractCollection的實現,程式碼也很簡單,不贅敘。
public synchronized boolean retainAll(Collection<?> c) { return super.retainAll(c); } public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); boolean modified = false; Iterator<E> it = iterator(); while (it.hasNext()) { if (!c.contains(it.next())) { it.remove(); modified = true; } } return modified; }
(32) public synchronized boolean addAll(int index, Collection<? extends E> c)
原始碼解釋:
在index位置插入集合c。實現原理和在某個位置插入元素原理一樣,不贅敘。
public synchronized boolean addAll(int index, Collection<? extends E> c) { modCount++; if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); int numMoved = elementCount - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); elementCount += numNew; return numNew != 0; }
(33) public synchronized List<E> subList(int fromIndex, int toIndex)
原始碼解釋:
呼叫父類的subList,然後通過Collections.synchronizedList來保證子List是同步的,這也就印證了我們前面所說的Collections.synchronizedList初始化的ArrayList和Vector是一樣效率的,因為它們的同步方式都是一樣的,而增刪改查這些操作對於它們兩個來說都是一樣的原理,所以可以知道它們的效率是一樣的。
public synchronized List<E> subList(int fromIndex, int toIndex) { return Collections.synchronizedList(super.subList(fromIndex, toIndex), this); }
(34) protected synchronized void removeRange(int fromIndex, int toIndex)
原始碼解釋:
將某個範圍的資料移除。實現原理和刪除某個位置的元素原理是一樣的,不贅敘。
protected synchronized void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = elementCount - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // Let gc do its work int newElementCount = elementCount - (toIndex-fromIndex); while (elementCount != newElementCount) elementData[--elementCount] = null; }
(35) public synchronized ListIterator<E> listIterator(int index)
原始碼解釋:
返回一個從index位置開始的LIstIterator,方便我們遍歷Vector,關於ListIterator在《Java容器類原始碼-LinkedList的最全的原始碼分析》已經詳說,這裡不贅敘。
public synchronized ListIterator<E> listIterator(int index) { if (index < 0 || index > elementCount) throw new IndexOutOfBoundsException("Index: "+index); return new ListItr(index); }
(36) public synchronized ListIterator<E> listIterator()
原始碼解釋:
返回一個從0位置開始的ListIterator,不贅敘。
public synchronized ListIterator<E> listIterator() { return new ListItr(0); }
(37) public synchronized Iterator<E> iterator()
原始碼解釋:
返回一個Iterator實現類Itr。有人會問ListIterator和Itr有什麼區別嗎?其實ListIterator是Itr的子類,它在Itr的基礎上再增加了一些介面,例如hasPrevious(),nextIndex()等,所以如果覺得Iterator不能滿足你的需求,可以看一下ListIterator裡面提供的API。
public synchronized Iterator<E> iterator() { return new Itr(); }
(38) public Spliterator<E> spliterator()
原始碼解釋:
例項化一個VectorSpliterator物件,並返回。VectorSpliterator是JDK1.8之後LinkedList新增的內部類,因為用得比較少,我就不在這裡班門弄斧了,大家有需要可以自行深入研究。
public Spliterator<E> spliterator() { return new VectorSpliterator<>(this, null, 0, -1, 0); }
四、總結
看完了Vector的原始碼,我覺得我們需要學到比較重要的幾點。首先是開頭所說的Vector和ArrayList的區別,這裡就不重複了。第二個就是我們通過subList這個實現可以看到,它的子序列其實也是通過Collections.synchronizedList來初始化子序列並返回的,所以其實Collections.synchronizedList初始化的ArrayList實現同步的原理和Vector是一樣的,而ArrayList和Vector的底層都是陣列,常規的增刪改查操作是一樣的,所以我們可以確定Vector和實現了同步的ArrayList在資料操作時的效率是相近的,所以我覺得我們並不需要糾結在考慮執行緒安全時到底是用Collections.synchronizedList初始化的ArrayList還是Vector。第三個就是需要了解到Vector的增刪改查實現原理,它的api核心可以說就是這幾個方法了,所有其他api都是圍繞這幾個方法來進行擴充套件。
---------------------
每天都在分享文章,也每天都有人想要我出來給大家分享下怎麼去學習Java。大家都知道,我們是學Java全棧的,大家就肯定以為我有全套的Java系統教程。沒錯,我是有Java全套系統教程,進扣裙【47】974【9726】所示,進群的時候記得表明自己想要學習什麼,不要用小號,這樣小編才好給你們發定向資源,今天小編就免費送!~
“我們相信人人都可以成為一個程式設計師,現在開始,找個師兄,帶你入門,學習的路上不再迷茫。這裡是ja+va修真院,初學者轉行到網際網路行業的聚集地。"