Iterable接口

本文主要对Iterable和Iterator两个接口进行深入学习。

1、Iterable接口

Iterable接口 (java.lang.Iterable) 是Java集合的顶级接口之一。我们首先看下这这个接口在JDK中的定义:

package java.lang; 

public interface Iterable<AnyType>
{ 
     Iterator<AnyType> iterator(); 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

(1)可见,Iterable接口中只包含一个方法,就是一个iterator()方法,用来返回一个Iterator类型的对象,或者说返回一个实现了Iterator接口的对象。

(2)实现了Iterable接口的类可以拥有增强的for循环,即只要实现了Iterable接口的类,就可以使用Iterator迭代器了。如

public static <AnyType> void print(Collection<AnyType> coll)
{
    for(AnyType item: coll)
        System.out.println(item);
}
  • 1
  • 2
  • 3
  • 4
  • 5

Tip:注意这里的函数输入参数Collection<AnyType> coll,其中coll是个接口变量。本来接口是不能用new来实例化一个接口的,即不能构造接口对象,但是可以用来声明接口变量,而且接口变量必须引用实现了接口的类对象。

(3)集合Collection、List、Set都是Iterable的实现类,所以他们及其他们的子类都可以使用foreach进行迭代。


好了,那么下面我们来看下Iterable接口中提到的Iterator接口吧。

2、Iterator接口

首先,我们还是先看下Iterator接口的定义:

package java.util; 
public interface Iterator<AnyType>
{ 
    boolean hasNext(); 
    AnyType next(); 
    void remove(); 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(1)当编译器见到一个正在用于Iterable对象的增强的for循环的时候,它用对iterator()方法的调用来代替增强的for循环以得到一个Iterator对象,然后调用next和hasNext。我们可以给出iterator()方法的定义

因此,上面Iterable例程可以改写为

public static <AnyType> void print(Collection<AnyType> coll)
{
    Iterator<AnyType> itr = coil.iterator();
    while(itr.hasNext())
    {
            AnyType item = itr.next();
            System.out.println(item);
    }       
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(2)Iterator接口中的核心方法next(),hasNext(),remove(),都是依赖当前位置。如果这些集合直接实现Iterator接口,则势必导致集合对象中包含当前迭代位置的数据(指针)。当集合在不同方法间进行传递的时候,由于当前迭代位置不可知,所以next()的结果也不可知。除非再为Iterator接口添加一个reset()方法,用来重置当前迭代位置。 而当实现Iterable则不然,每次调用都返回一个从头开始的迭代器,各个迭代器之间互不影响。

3、综合应用

我们后面会自己实现数据结构ArrayList,其中就用到了Iterable接口和Iterator接口,我们看下这个综合的例子

    //  Obtains an Iterator object used to traverse the collection
    public java.util.Iterator<AnyType> iterator()
    {
        return new ArrayListIterator();
    }


    /**
    * This is the implementation of the ArrayListIterator.
    * It maintains a notion of a current position and 
    * the implicit reference to the MyArrayList.
    **/
    private class ArrayListIterator implements java.util.Iterator<AnyType>
    {
        private int current = 0;
        private boolean okToRemove = false;

        public boolean hasNest()
        {
            return current<size();
        }

        public AnyType next()
        {
            if(!hasNext())
                throw new java.util.NoSuchElementException();
            okToRemove = true;
            return theItems[current++];
        }

        public void remove()
        {
            if(!okToRemove)
                throw new IllegalStateException();
            MyArrayList.this.remove(--current);
            okToRemove = false;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

其中,iterator()方法就直接返回ArrayListIterator类的一个实例,该类是一个实现Iterator接口的类。

lucene中的document类实现

public final class Document implements Iterable<IndexableField> {

  //以ArrayList来保存不同的Field内容
  private final List<IndexableField> fields = new ArrayList<>();

  /** Constructs a new document with no fields. */
  public Document() {}

  @Override
  public Iterator<IndexableField> iterator() {
    return fields.iterator();
  }
。。。。。。
}

猜你喜欢

转载自blog.csdn.net/asdfsadfasdfsa/article/details/80146091