從Java Collections源碼分析迭代器模式

原文:http://blog.csdn.net/mudalu626/article/details/6457427

一、 引言
  
  迭代這個名詞對於熟悉Java的人來說絕對不陌生。我們常常使用JDK提供的迭代接口進行java collection的遍歷:

  
  Iterator it = list.iterator();
  while(it.hasNext()){
  //using “it.next();”do some businesss logic
  }
  
  而這就是關於迭代器模式應用很好的例子。
  
  二、 定義與結構
  
  迭代器(Iterator)模式,又叫做遊標(Cursor)模式。GOF給出的定義爲:提供一種方法訪問一個容器(container)對象各個元素,而又不需暴露該對象的內部細節

  
  從定義可見,迭代器模式是爲容器而生。很明顯,對容器對象的訪問必然涉及到遍歷算法。你可以一股腦的將遍歷方法塞到容器對象中去;或者根本不去提供什麼遍歷算法,讓使用容器的人自己去實現去吧。這兩種情況好像都能夠解決問題。
  
  然而在前一種情況,容器承受了過多的功能,它不僅要負責自己“容器”內的元素維護(添加、刪除等等),而且還要提供遍歷自身的接口;而且由於遍歷狀態保存的問題,不能對同一個容器對象同時進行多個遍歷。第二種方式倒是省事,卻又將容器的內部細節暴露無遺。
  
  而迭代器模式的出現,很好的解決了上面兩種情況的弊端。先來看下迭代器模式的真面目吧。
  
  迭代器模式由以下角色組成:
  
  1) 迭代器角色(Iterator):迭代器角色負責定義訪問和遍歷元素的接口。
  
  2) 具體迭代器角色(Concrete Iterator):具體迭代器角色要實現迭代器接口,並要記錄遍歷中的當前位置。
  
  3) 容器角色(Container):容器角色負責提供創建具體迭代器角色的接口。
  
  4) 具體容器角色(Concrete Container):具體容器角色實現創建具體迭代器角色的接口——這個具體迭代器角色於該容器的結構相關。
  
  迭代器模式的類圖如下:

 

迭代器模式類圖

 

  

  三、 從Java Collections源碼分析迭代器模式


  下面我們拿出java中Iterator的源碼相關源碼進行分析。

  下圖是Java Collections的整體關係圖:

 

Java Collection

 

  從圖中很容易看出,Collection和代表相應的抽象容器角色,而Iterator和ListIterator則代表相應的抽象迭代器角色。

  其中ListIterator是Iterator的子接口,擴充了向後遍歷的方法接口。

  以下是Collection,Iterator,List及ListIterator的代碼片段:

Collection:

  1. //Collection繼承自Iterable接口,而Iterable就是用來生產Iterator的接口  
  2. public interface Collection<E> extends Iterable<E> {  
  3. ...  
  4.     //繼承自Iterable的方法,實現Collection和Iterator的耦合  
  5.     Iterator<E> iterator();  
  6. ...  
  7. }  
 

Iterator:

  1. public interface Iterator<E> {  
  2.     boolean hasNext();//如果仍有元素可以迭代,則返回 true。  
  3.     E next();//返回迭代的下一個元素。  
  4.     void remove();//從迭代器指向的集合中移除迭代器返回的最後一個元素(可選操作)。  
  5. }  
 

List:

  1. //List繼承自Collection  
  2. public interface List<E> extends Collection<E> {  
  3.     Iterator<E> iterator();//List和Iterator的耦合  
  4.     ListIterator<E> listIterator();//List和ListIterator的耦合  
  5.     ListIterator<E> listIterator(int index);//從列表的指定位置開始  
  6. }  
 

ListIterator:

  1. //ListIterator繼承自Iterator  
  2. public interface ListIterator<E> extends Iterator<E> {  
  3.     boolean hasNext();//以正向遍歷列表時,如果列表迭代器有多個元素,則返回 true(換句話說,如果 next 返回一個元素而不是拋出異常,則返回 true)  
  4.     E next();//返回列表中的下一個元素  
  5.     boolean hasPrevious();//如果以反向遍歷列表,列表迭代器有多個元素,則返回 true  
  6.     E previous();//返回列表中的前一個元素  
  7.     int nextIndex();//返回對 next 的後續調用所返回元素的索引  
  8.     int previousIndex();//返回對 previous 的後續調用所返回元素的索引  
  9.     void remove();//從列表中移除由 next 或 previous 返回的最後一個元素(可選操作)  
  10.     void set(E e);//用指定元素替換 next 或 previous 返回的最後一個元素(可選操作)  
  11.     void add(E e);//將指定的元素插入列表(可選操作)  
  12. }  
 

  那麼具體容器角色和具體迭代器角色是那些類呢?

  首先先從Iterator入手,讓我們先回到圖上,熟悉Java Collections框架的朋友很容易明白Collection的實現類很多,它是Java Collections框架的頂層結構,它的下層又分Set和List兩個分支,那我們就分別來分析這兩個分支具體的具體實現。


  List的第一級實現類爲AbstractList,java在此類中實現了具體容器角色和具體迭代器角色,那如何能在一個類中實現兩個角色呢,還是從代碼入手吧:

  1. //AbstractList實現類List接口  
  2. public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {  
  3.     ...  
  4.     //已從結構上修改 此列表的次數。從結構上修改是指更改列表的大小,或者以其他方式打亂列表,使正在進行的迭代產生錯誤的結果  
  5.     protected transient int modCount = 0;  
  6.     ...  
  7.     //生產迭代器,每次生產一個新的對象,所以對同一個容器對象,可以同時進行多個遍歷  
  8.     public Iterator<E> iterator() {  
  9.     return new Itr();  
  10.     }  
  11.     ...  
  12.     //這就是解決一個類實現兩個角色的關鍵,用了一個私有內部類來實現了Iterator  
  13.     private class Itr implements Iterator<E> {  
  14.         //遊標,也就是被遍歷具體容器(AbstractList)的當前索引值  
  15.     int cursor = 0;  
  16.         //離遊標最近的索引值,主要用在remove方法中,下面會詳細說明  
  17.     int lastRet = -1;  
  18.         //此變量用作跟蹤具體容器(AbstractList)的modCount變量,如果不一致則報錯  
  19.     int expectedModCount = modCount;  
  20.         //原來hasNext的實現如此簡單,用遊標和具體容器的size進行比較即可,遊標的操作見next方法  
  21.     public boolean hasNext() {  
  22.             return cursor != size();  
  23.     }  
  24.         //next方法也不難嘛,就是利用遊標一次一次對具體容器進行遍歷  
  25.     public E next() {  
  26.             //檢查迭代器運行期間,容器結構是否修改,如修改則報錯  
  27.             checkForComodification();  
  28.         try {  
  29.                 //呵呵,終於找到你了,原來又是用具體容器的方法實現的,這就是隱藏容器細節的關鍵  
  30.         E next = get(cursor);  
  31.                 //記錄被返回的索引位置,並使遊標向前移動  
  32.         lastRet = cursor++;  
  33.                 //返回被迭代的當前對象  
  34.         return next;  
  35.         } catch (IndexOutOfBoundsException e) {  
  36.         checkForComodification();  
  37.         throw new NoSuchElementException();  
  38.         }  
  39.     }  
  40.         //不用說了,remove方法的實現肯定也是借了容器之力  
  41.     public void remove() {  
  42.         if (lastRet == -1)  
  43.         throw new IllegalStateException();  
  44.             checkForComodification();  
  45.         try {  
  46.                 //嘿嘿,又是隱藏細節  
  47.         AbstractList.this.remove(lastRet);  
  48.         if (lastRet < cursor)  
  49.             cursor--;  
  50.         lastRet = -1;  
  51.                 //因爲remove方法改變了容器結構,所以需要同步一下expectedModCount變量使修改次數一致  
  52.         expectedModCount = modCount;  
  53.         } catch (IndexOutOfBoundsException e) {  
  54.         throw new ConcurrentModificationException();  
  55.         }  
  56.     }  
  57.         //檢查迭代器運行期間,容器結構是否修改,如修改則報錯  
  58.     final void checkForComodification() {  
  59.         if (modCount != expectedModCount)  
  60.         throw new ConcurrentModificationException();  
  61.     }  
  62.     }  
  63.     ...  
  64.     //同理,ListIterator的實現類也作爲容器的私有內部類出現  
  65.     private class ListItr extends Itr implements ListIterator<E> {  
  66.         //具體實現就不介紹了,和Iterator大同小異  
  67.     ListItr(int index) {  
  68.         cursor = index;  
  69.     }  
  70.     public boolean hasPrevious() {  
  71.         return cursor != 0;  
  72.     }  
  73.         public E previous() {  
  74.             checkForComodification();  
  75.             try {  
  76.                 int i = cursor - 1;  
  77.                 E previous = get(i);  
  78.                 lastRet = cursor = i;  
  79.                 return previous;  
  80.             } catch (IndexOutOfBoundsException e) {  
  81.                 checkForComodification();  
  82.                 throw new NoSuchElementException();  
  83.             }  
  84.         }  
  85.     public int nextIndex() {  
  86.         return cursor;  
  87.     }  
  88.     public int previousIndex() {  
  89.         return cursor-1;  
  90.     }  
  91.     public void set(E e) {  
  92.         if (lastRet == -1)  
  93.         throw new IllegalStateException();  
  94.             checkForComodification();  
  95.         try {  
  96.         AbstractList.this.set(lastRet, e);  
  97.         expectedModCount = modCount;  
  98.         } catch (IndexOutOfBoundsException ex) {  
  99.         throw new ConcurrentModificationException();  
  100.         }  
  101.     }  
  102.     public void add(E e) {  
  103.             checkForComodification();  
  104.         try {  
  105.         AbstractList.this.add(cursor++, e);  
  106.         lastRet = -1;  
  107.         expectedModCount = modCount;  
  108.         } catch (IndexOutOfBoundsException ex) {  
  109.         throw new ConcurrentModificationException();  
  110.         }  
  111.     }  
  112.     }  
  113. }  

      對於再下層的實現類,可以靈活的重寫生產方法,並實現專用的迭代器內部類。

 

  Set和List不同,它的下一層沒有實現類,而是倆個接口(AbstractSet和SortedSet),再往下一層找便是我們熟悉的兩個實現類HashSet和TreeSet,而瞭解Java Collection的讀者會知道,HashSet和TreeSet其實是藉助Map系的HashMap和TreeMap實現的,所以具體實現方法是在Map系中的實現類中實現的(隱藏夠深的)。因爲涉及的兩個對象數據結構稍微有些複雜(散列表和紅黑樹),可以在此處瞭解其內部構造。廢話少說,上代碼:

  1. public class HashMap<K,V>  
  2.     extends AbstractMap<K,V>  
  3.     implements Map<K,V>, Cloneable, Serializable  
  4. {  
  5.     ...  
  6.     //已從結構上修改 此列表的次數。  
  7.     transient volatile int modCount;  
  8.     ...  
  9.     //因散列表結構的特殊性,需要分別實現三個迭代器KeyIterator、ValueIterator和EntryIterator,基類爲HashIterator  
  10.     //基類爲HashIterator  
  11.     private abstract class HashIterator<E> implements Iterator<E> {  
  12.         Entry<K,V> next;  // 散列表的鏈表的下一個Entry對象  
  13.         int expectedModCount;   // 與modCount同步的變量  
  14.         int index;      // 散列表的數組索引  
  15.         Entry<K,V> current;   // 散列表的鏈表的當前Entry對象  
  16.         //構造函數  
  17.         HashIterator() {  
  18.         //同步expectedModCount  
  19.             expectedModCount = modCount;  
  20.             if (size > 0) {   
  21.         //取得散列表的數組  
  22.                 Entry[] t = table;  
  23.         //取得取得散列表的數組中不爲空的項目索引並將數組中的鏈表頭賦值給next  
  24.                 while (index < t.length && (next = t[index++]) == null)  
  25.                     ;  
  26.             }  
  27.         }  
  28.     //雖然沒看到對HashMap的方法調用,但是在構造函數中已經和HashMap對象通信  
  29.         public final boolean hasNext() {  
  30.             return next != null;  
  31.         }  
  32.     //通過分別遍歷散列表中的當前鏈表和數組,查找下一個Entry對象  
  33.         final Entry<K,V> nextEntry() {  
  34.         //檢查迭代器運行期間,容器結構是否修改,如修改則報錯  
  35.             if (modCount != expectedModCount)  
  36.                 throw new ConcurrentModificationException();  
  37.         //取得遍歷散列表中的當前鏈表  
  38.             Entry<K,V> e = next;  
  39.             if (e == null)  
  40.                 throw new NoSuchElementException();  
  41.         //如果當前鏈表遍歷到尾,則繼續對數組進行遍歷  
  42.             if ((next = e.next) == null) {  
  43.                 Entry[] t = table;  
  44.                 while (index < t.length && (next = t[index++]) == null)  
  45.                     ;  
  46.             }  
  47.         //取得最新的鏈表  
  48.         current = e;  
  49.         //返回Entry對象  
  50.             return e;  
  51.         }  
  52.     //使用了HashMap對象的removeEntryForKey進行刪除操作,又是隱藏細節。。。  
  53.         public void remove() {  
  54.             if (current == null)  
  55.                 throw new IllegalStateException();  
  56.             if (modCount != expectedModCount)  
  57.                 throw new ConcurrentModificationException();  
  58.             Object k = current.key;  
  59.             current = null;  
  60.             HashMap.this.removeEntryForKey(k);  
  61.             expectedModCount = modCount;  
  62.         }  
  63.     }  
  64.     //迭代器的具體實現類ValueIterator,繼承自HashIterator  
  65.     private final class ValueIterator extends HashIterator<V> {  
  66.     //實現next方法,返回Entry對象的Value  
  67.         public V next() {  
  68.             return nextEntry().value;  
  69.         }  
  70.     }  
  71.     //迭代器的具體實現類KeyIterator,繼承自HashIterator  
  72.     private final class KeyIterator extends HashIterator<K> {  
  73.     //實現next方法,返回Entry對象的Key  
  74.         public K next() {  
  75.             return nextEntry().getKey();  
  76.         }  
  77.     }  
  78.     //迭代器的具體實現類EntryIterator,繼承自HashIterator  
  79.     private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {  
  80.     //實現next方法,返回Entry對象  
  81.         public Map.Entry<K,V> next() {  
  82.             return nextEntry();  
  83.         }  
  84.     }  
  85.     //三個工廠方法分別製造三個不同的迭代器  
  86.     Iterator<K> newKeyIterator()   {  
  87.         return new KeyIterator();  
  88.     }  
  89.     Iterator<V> newValueIterator()   {  
  90.         return new ValueIterator();  
  91.     }  
  92.     Iterator<Map.Entry<K,V>> newEntryIterator()   {  
  93.         return new EntryIterator();  
  94.     }  
  95.       
  96.     //因上面三個迭代器爲HashMap專用,所以不適合Set(ValueIterator和EntryIterator),所以在HashMap的內部類KeySet中又使用了HashMap的KeyIterator,所以HashSet的具體迭代器其實就是HashMap的KeyIterator迭代器  
  97.     private final class KeySet extends AbstractSet<K> {  
  98.         public Iterator<K> iterator() {  
  99.             return newKeyIterator();  
  100.         }  
  101.         public int size() {  
  102.             return size;  
  103.         }  
  104.         public boolean contains(Object o) {  
  105.             return containsKey(o);  
  106.         }  
  107.         public boolean remove(Object o) {  
  108.             return HashMap.this.removeEntryForKey(o) != null;  
  109.         }  
  110.         public void clear() {  
  111.             HashMap.this.clear();  
  112.         }  
  113.     }  
  114.     private transient Set<Map.Entry<K,V>> entrySet = null;  
  115.     public Set<K> keySet() {  
  116.         Set<K> ks = keySet;  
  117.         return (ks != null ? ks : (keySet = new KeySet()));  
  118.     }  
  119. }  
 

  TreeSet的迭代器實現和HashMap很相似,在這裏就不再羅嗦了,有興趣的讀者可以去研讀一下源碼。

      對於再下層的實現類,可以靈活的重寫生產方法,並實現專用的迭代器內部類。

 

  三、 總結


  從代碼我們可以得出以下幾點:

      1.具體容器角色和具體迭代器角色因爲是緊耦合關係,在具體容器中使用內部類將具體迭代器實現是一個很好的做法,方便了具體迭代器直接訪問具體容器的所有細節。

      2.內部類爲私有,既實現類具體迭代器,又使此容器的專用迭代器對外不可見,迫使使用者進行接口編程。

          3.使用迭代器訪問一個容器對象的內容而無需暴露它的內部表示。

          4.對同一個容器對象,可以同時進行多個遍歷。

          5.支持以不同的方式遍歷一個容器角色。根據實現方式的不同,效果上會有差別。

          6.簡化了容器的接口。但是在java Collection中爲了提高可擴展性,容器還是提供了遍歷的接口。

          7.爲遍歷不同的容器結構提供一個統一的接口。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章