我們經常使用subString方法來對String對象進行分割處理,同時我們也可以使用subList、subMap、subSet來對List、Map、Set進行分割處理,但是這個分割存在某些瑕疵。
一、subList返回僅僅只是一個視圖
首先我們先看如下實例:
public static void main(String[] args) { List<Integer> list1 = new ArrayList<Integer>(); list1.add(1); list1.add(2);</span><span style="color: #008000">//</span><span style="color: #008000">通過構造函數新建一個包含list1的列表 list2</span> List<Integer> list2 = <span style="color: #0000ff">new</span> ArrayList<Integer><span style="color: #000000">(list1); </span><span style="color: #008000">//</span><span style="color: #008000">通過subList生成一個與list1一樣的列表 list3</span> List<Integer> list3 = list1.subList(0<span style="color: #000000">, list1.size()); </span><span style="color: #008000">//</span><span style="color: #008000">修改list3</span> list3.add(3<span style="color: #000000">); System.out.println(</span>"list1 == list2:" +<span style="color: #000000"> list1.equals(list2)); System.out.println(</span>"list1 == list3:" +<span style="color: #000000"> list1.equals(list3)); }</span></pre>
這個例子非常簡單,無非就是通過構造函數、subList重新生成一個與list1一樣的list,然后修改list3,最后比較list1 == list2?、list1 == list3?。按照我們常規的思路應該是這樣的:因為list3通過add新增了一個元素,那么它肯定與list1不等,而list2是通過list1構造出來的,所以應該相等,所以結果應該是:
list1 == list2:true list1 == list3: false
首先我們先不論結果的正確與否,我們先看subList的源碼:
public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); }
subListRangeCheck方式是判斷fromIndex、toIndex是否合法,如果合法就直接返回一個subList對象,注意在產生該new該對象的時候傳遞了一個參數 this ,該參數非常重要,因為他代表着原始list。
/** * 繼承AbstractList類,實現RandomAccess接口 */ private class SubList extends AbstractList<E> implements RandomAccess { private final AbstractList<E> parent; //列表 private final int parentOffset; private final int offset; int size;</span><span style="color: #008000">//</span><span style="color: #008000">構造函數</span> SubList(AbstractList<E><span style="color: #000000"> parent, </span><span style="color: #0000ff">int</span> offset, <span style="color: #0000ff">int</span> fromIndex, <span style="color: #0000ff">int</span><span style="color: #000000"> toIndex) { </span><span style="color: #0000ff">this</span>.parent =<span style="color: #000000"> parent; </span><span style="color: #0000ff">this</span>.parentOffset =<span style="color: #000000"> fromIndex; </span><span style="color: #0000ff">this</span>.offset = offset +<span style="color: #000000"> fromIndex; </span><span style="color: #0000ff">this</span>.size = toIndex -<span style="color: #000000"> fromIndex; </span><span style="color: #0000ff">this</span>.modCount = ArrayList.<span style="color: #0000ff">this</span><span style="color: #000000">.modCount; } </span><span style="color: #008000">//</span><span style="color: #008000">set方法</span> <span style="color: #0000ff">public</span> E set(<span style="color: #0000ff">int</span><span style="color: #000000"> index, E e) { rangeCheck(index); checkForComodification(); E oldValue </span>= ArrayList.<span style="color: #0000ff">this</span>.elementData(offset +<span style="color: #000000"> index); ArrayList.</span><span style="color: #0000ff">this</span>.elementData[offset + index] =<span style="color: #000000"> e; </span><span style="color: #0000ff">return</span><span style="color: #000000"> oldValue; } </span><span style="color: #008000">//</span><span style="color: #008000">get方法</span> <span style="color: #0000ff">public</span> E get(<span style="color: #0000ff">int</span><span style="color: #000000"> index) { rangeCheck(index); checkForComodification(); </span><span style="color: #0000ff">return</span> ArrayList.<span style="color: #0000ff">this</span>.elementData(offset +<span style="color: #000000"> index); } </span><span style="color: #008000">//</span><span style="color: #008000">add方法</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> add(<span style="color: #0000ff">int</span><span style="color: #000000"> index, E e) { rangeCheckForAdd(index); checkForComodification(); parent.add(parentOffset </span>+<span style="color: #000000"> index, e); </span><span style="color: #0000ff">this</span>.modCount =<span style="color: #000000"> parent.modCount; </span><span style="color: #0000ff">this</span>.size++<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000">remove方法</span> <span style="color: #0000ff">public</span> E remove(<span style="color: #0000ff">int</span><span style="color: #000000"> index) { rangeCheck(index); checkForComodification(); E result </span>= parent.remove(parentOffset +<span style="color: #000000"> index); </span><span style="color: #0000ff">this</span>.modCount =<span style="color: #000000"> parent.modCount; </span><span style="color: #0000ff">this</span>.size--<span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } }</span></pre>
該SubLsit是ArrayList的內部類,它與ArrayList一樣,都是繼承AbstractList和實現RandomAccess接口。同時也提供了get、set、add、remove等list常用的方法。但是它的構造函數有點特殊,在該構造函數中有兩個地方需要注意:
1、this.parent = parent;而parent就是在前面傳遞過來的list,也就是說this.parent就是原始list的引用。
2、this.offset = offset + fromIndex;this.parentOffset = fromIndex;。同時在構造函數中它甚至將modCount(fail-fast機制)傳遞過來了。
我們再看get方法,在get方法中return ArrayList.this.elementData(offset + index);這段代碼可以清晰表明get所返回就是原列表offset + index位置的元素。同樣的道理還有add方法里面的:
parent.add(parentOffset + index, e); this.modCount = parent.modCount;
remove方法里面的
E result = parent.remove(parentOffset + index); this.modCount = parent.modCount;
誠然,到了這里我們可以判斷subList返回的SubList同樣也是AbstractList的子類,同時它的方法如get、set、add、remove等都是在原列表上面做操作,它並沒有像subString一樣生成一個新的對象。所以subList返回的只是原列表的一個視圖,它所有的操作最終都會作用在原列表上。
那么從這里的分析我們可以得出上面的結果應該恰恰與我們上面的答案相反:
list1 == list2:false list1 == list3:true
Java細節(3.1):subList返回的只是原列表的一個視圖,它所有的操作最終都會作用在原列表上
二、subList生成子列表后,不要試圖去操作原列表
從上面我們知道subList生成的子列表只是原列表的一個視圖而已,如果我們操作子列表它產生的作用都會在原列表上面表現,但是如果我們操作原列表會產生什么情況呢?
public static void main(String[] args) { List<Integer> list1 = new ArrayList<Integer>(); list1.add(1); list1.add(2);</span><span style="color: #008000">//</span><span style="color: #008000">通過subList生成一個與list1一樣的列表 list3</span> List<Integer> list3 = list1.subList(0<span style="color: #000000">, list1.size()); </span><span style="color: #008000">//</span><span style="color: #008000">修改list3</span> list1.add(3<span style="color: #000000">); System.out.println(</span>"list1'size:" +<span style="color: #000000"> list1.size()); System.out.println(</span>"list3'size:" +<span style="color: #000000"> list3.size()); }</span></pre>
該實例如果不產生意外,那么他們兩個list的大小都應該都是3,但是偏偏事與願違,事實上我們得到的結果是這樣的:
list1'size:3
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(Unknown Source)
at java.util.ArrayList$SubList.size(Unknown Source)
at com.chenssy.test.arrayList.SubListTest.main(SubListTest.java:17)
list1正常輸出,但是list3就拋出ConcurrentModificationException異常,看過我另一篇博客的同仁肯定對這個異常非常,fail-fast?不錯就是fail-fast機制,在fail-fast機制中,LZ花了很多力氣來講述這個異常,所以這里LZ就不對這個異常多講了(更多請點這里:Java提高篇(三四)—–fail-fast機制)。我們再看size方法:
public int size() { checkForComodification(); return this.size; }
size方法首先會通過checkForComodification驗證,然后再返回this.size。
private void checkForComodification() { if (ArrayList.this.modCount != this.modCount) throw new ConcurrentModificationException(); }
該方法表明當原列表的modCount與this.modCount不相等時就會拋出ConcurrentModificationException。同時我們知道modCount 在new的過程中 "繼承"了原列表modCount,只有在修改該列表(子列表)時才會修改該值(先表現在原列表后作用於子列表)。而在該實例中我們是操作原列表,原列表的modCount當然不會反應在子列表的modCount上啦,所以才會拋出該異常。
對於子列表視圖,它是動態生成的,生成之后就不要操作原列表了,否則必然都導致視圖的不穩定而拋出異常。最好的辦法就是將原列表設置為只讀狀態,要操作就操作子列表:
//通過subList生成一個與list1一樣的列表 list3 List<Integer> list3 = list1.subList(0, list1.size());//對list1設置為只讀狀態
list1 = Collections.unmodifiableList(list1);
Java細節(3.2):生成子列表后,不要試圖去操作原列表,否則會造成子列表的不穩定而產生異常
三、推薦使用subList處理局部列表
在開發過程中我們一定會遇到這樣一個問題:獲取一堆數據后,需要刪除某段數據。例如,有一個列表存在1000條記錄,我們需要刪除100-200位置處的數據,可能我們會這樣處理:
for(int i = 0 ; i < list1.size() ; i++){ if(i >= 100 && i <= 200){ list1.remove(i); /* * 當然這段代碼存在問題,list remove之后后面的元素會填充上來, * 所以需要對i進行簡單的處理,當然這個不是這里討論的問題。 */ } }
這個應該是我們大部分人的處理方式吧,其實還有更好的方法,利用subList。在前面LZ已經講過,子列表的操作都會反映在原列表上。所以下面一行代碼全部搞定:
list1.subList(100, 200).clear();
簡單而不失華麗!!!!!
參考資料:編寫高質量代碼:改善Java程序的151個建議
-----原文出自:http://cmsblogs.com/?p=1239,請尊重作者辛勤勞動成果,轉載說明出處.
-----個人站點:http://cmsblogs.com