Iterator、Iterable介面的使用及詳解
Java集合類庫將集合的介面與實現分離。同樣的介面,可以有不同的實現。
Java集合類的基本介面是Collection介面。而Collection介面必須實現Iterator介面。
以下圖表示集合框架的介面,java.lang以及java.util兩個包裡的。其他部分可以從左向右看,比如Collection的Subinterfaces有List,Set以及Queue等。
package java.util; /** * An iterator over a collection. Iterator takes the place of Enumeration in * the Java collections framework. Iterators differ from enumerations in two * ways: <ul> * <li> Iterators allow the caller to remove elementsfrom the * underlying collection during the iteration with well-defined * semantics. * <li> Method names have been improved. * </ul><p> * * This interface is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * *@author Josh Bloch * @version %I%, %G% * @see Collection * @see ListIterator * @see Enumeration * @since 1.2 */ public interface Iterator<E> { /** * Returns <tt>true</tt> if the iteration has more elements. (In other * words, returns <tt>true</tt> if <tt>next</tt> would return an element * rather than throwing an exception.) * *@return <tt>true</tt> if the iterator has more elements. */ boolean hasNext(); /** * Returns the next element (每一次迭代,the next element就是index為0的元素)in the iteration. * * @return the next element in the iteration. * @exception NoSuchElementException iteration has no more elements. */ E next(); /** * * Removes from the underlying collection the last element returned by the * iterator (optional operation). This method can be called only once per * call to <tt>next</tt>. The behavior of an iterator is unspecified if * the underlying collection is modified while the iteration is in * progress in any way other than by calling this method. * * @exception UnsupportedOperationException if the <tt>remove</tt> * operation is not supported by this Iterator. * @exception IllegalStateException if the <tt>next</tt> method has not * yet been called, or the <tt>remove</tt> method has already * been called after the last call to the <tt>next</tt> * method. */ void remove(); }
以下例子是利用了Iterator介面的著三個方法,實現遍歷ArrayList<String>型別。
一開始迭代器在所有元素的左邊,呼叫next()之後,迭代器移到第一個和第二個元素之間,next()方法返回迭代器剛剛經過的元素。
hasNext()若返回True,則表明接下來還有元素,迭代器不在尾部。
remove()方法必須和next方法一起使用,功能是去除剛剛next方法返回的元素。
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class ForEachDemo { public static void main(String... arg) { Collection<String> a = new ArrayList<String>(); a.add("Bob"); a.add("Alice"); a.add("Lisy"); Iterator<String> iterator = a.iterator(); while (iterator.hasNext()) { String ele = iterator.next(); System.out.println(ele);//Bob Alice Lisy } System.out.println(a);//[Bob, Alice, Lisy] iterator = a.iterator(); iterator.next(); iterator.remove(); System.out.println(a);//[Alice, Lisy] } }
package java.lang; import java.util.Iterator; /** Implementing this interface allows an object to be the target of * the "foreach" statement. * @since 1.5 */ public interface Iterable<T> { /** * Returns an iterator over a set of elements of type T. * * @return an Iterator. */ Iterator<T> iterator(); }
for-each迴圈可以與任何實現了Iterable介面的物件一起工作。
而Collection介面擴充套件了Iterable介面,故標準類庫中的任何集合都可以使用for-each迴圈。
Collection介面
此介面的方法
public interface Collection<E>{......}
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Ensures that this collection contains the specified element (optional operation).
|
boolean |
Adds all of the elements in the specified collection to this collection (optional operation). |
void |
clear()
Removes all of the elements from this collection (optional operation).
|
boolean |
Returns true if this collection contains the specified element. |
boolean |
Returns true if this collection contains all of the elements in the specified collection. |
boolean |
Compares the specified object with this collection for equality. |
int |
Returns the hash code value for this collection. |
boolean |
Returns true if this collection contains no elements. |
Returns an iterator over the elements in this collection. | |
boolean |
Removes a single instance of the specified element from this collection, if it is present (optional operation). |
boolean |
Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
boolean |
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
int |
size()
Returns the number of elements in this collection.
|
Returns an array containing all of the elements in this collection. | |
<T> T[] |
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
因為其中有一個返回值為Iterator<E>型別的iterator()方法,所以,Collection介面必須實現Iterator介面
實現Collection介面的每一個類都要實現以上眾多方法,但開發者自己實現很麻煩。所以java提供了AbstractCollection類來編寫具體的類。
java.util
Interface Collection<E>
- All Superinterfaces:
Collection介面有三個常用的子介面,分別是List,Set,Queue。
http://blog.csdn.net/xujinsmile/article/details/8543544
看一下JDK中的集合類,比如List一族或者Set一族,
都是實現了Iterable介面,但並不直接實現Iterator介面。
仔細想一下這麼做是有道理的。因為Iterator介面的核心方法next()或者hasNext()
是依賴於迭代器的當前迭代位置的。
如果Collection直接實現Iterator介面,勢必導致集合物件中包含當前迭代位置的資料(指標)。
當集合在不同方法間被傳遞時,由於當前迭代位置不可預置,那麼next()方法的結果會變成不可預知。
除非再為Iterator介面新增一個reset()方法,用來重置當前迭代位置。
但即時這樣,Collection也只能同時存在一個當前迭代位置。
而Iterable則不然,每次呼叫都會返回一個從頭開始計數的迭代器。
多個迭代器是互不干擾的。
http://www.cnblogs.com/highriver/archive/2011/07/27/2077913.html
import java.util.Iterator; public class ForEachAPIDemo { public static void main(String[] args) throws Exception { Students students = new Students(10); for (Student student : students) { System.out.println(student.getSid() + ":" + student.getName()); } } } // 支援for each迭代迴圈的學生集合類 class Students implements Iterable<Student> { // 儲存所有學生類的陣列 private Student[] students; // 該建構函式可以生成指定大小的學生類變數陣列,並初始化該學生類變數陣列 public Students(int size) { students = new Student[size]; for (int i = 0; i < size; i++) { students[i] = new Student(String.valueOf(i), "學生" + String.valueOf(i)); } } @Override public Iterator<Student> iterator() { return new StudentIterator(); } // 實現Iterator介面的私有內部類,外界無法直接訪問 private class StudentIterator implements Iterator<Student> { // 當前迭代元素的下標 private int index = 0; // 判斷是否還有下一個元素,如果迭代到最後一個元素就返回false public boolean hasNext() { return index != students.length; } @Override public Student next() { return students[index++]; } // 這裡不支援,丟擲不支援操作異常 public void remove() { throw new UnsupportedOperationException(); } } } class Student { private String sid; private String name; public Student(String sid, String name) { setSid(sid); setName(name); } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "sid='" + sid + '\'' + ", name='" + name + '\'' + '}'; } }