2017-07-01 15 views
-1

私はJavaに新しいので、多分あなたの一部に私の質問は馬鹿に見えるでしょう。Iteratorインターフェイスが必要なのはなぜですか?なぜそれを使用する必要がありますか?

私は私のカスタムオブジェクトforeachを作る必要がある場合、いくつかのチュートリアルから理解するように、オブジェクトはIterableインターフェイスを実装する必要があります。

なぜ私はIteratorインターフェイスが必要なのですか?なぜそれを使用する必要がありますか?

答えて

3

前述のとおり、Iterableはforeachループで使用されます。

foreachループですべてが使用できるわけではありません。あなたはこれが何をすると思いますか?

for (int a : 10) 

Javaの設計者は、このナンセンスを見つけてコンパイラエラーとして報告することができるようにしたいと考えました。だから、彼らは "foreachループではどんなものが使われるのですか?" "まあ"、彼らは "オブジェクトはイテレータを返すことができなければならない"と考えていました。そして、このインターフェースが生まれている:

public interface Iterable<T> { 
    /** 
    * Returns an iterator over elements of type {@code T}. 
    * 
    * @return an Iterator. 
    */ 
    Iterator<T> iterator(); 
} 

コンパイラは、単にforeachループ内のオブジェクトがIterableかを実装しているかどうかを確認することができます。そうでない場合は、エラーを吐き出す。コンパイラにとって、これは「はい、私は繰り返すことができます」と言う種類の「マーカー」と考えることができます。

「イテレータは何ですか?」と彼らは再び考えました。「イテレータは次の要素を返すことができ、次の要素があるかどうかを返すことができるはずです。イテレータの中には、 。したがって、このインタフェースが生まれ:Iteratorはデザインパターンである

public interface Iterator<E> { 
    /** 
    * Returns {@code true} if the iteration has more elements. 
    * (In other words, returns {@code true} if {@link #next} would 
    * return an element rather than throwing an exception.) 
    * 
    * @return {@code true} if the iteration has more elements 
    */ 
    boolean hasNext(); 

    /** 
    * Returns the next element in the iteration. 
    * 
    * @return the next element in the iteration 
    * @throws NoSuchElementException if the iteration has no more elements 
    */ 
    E next(); 

    /** 
    * Removes from the underlying collection the last element returned 
    * by this iterator (optional operation). This method can be called 
    * only once per call to {@link #next}. 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. 
    * 
    * @implSpec 
    * The default implementation throws an instance of 
    * {@link UnsupportedOperationException} and performs no other action. 
    * 
    * @throws UnsupportedOperationException if the {@code remove} 
    *   operation is not supported by this iterator 
    * 
    * @throws IllegalStateException if the {@code next} method has not 
    *   yet been called, or the {@code remove} method has already 
    *   been called after the last call to the {@code next} 
    *   method 
    */ 
    default void remove() { 
     throw new UnsupportedOperationException("remove"); 
    } 
} 
1

、これはまた、ユーザーから店の要素と繰り返し機構の実装を非表示にすることができ、特定の方法で同じオブジェクトのコレクションを通過することができます。 javadocで見られるように、多くのクラスでは、コレクションだけでなく、Itarableインターフェースも実装されています。例では、同じパフォーマンスで2つのListの実装を反復することができます。ArrayListは同じ時間にインデックスを指定しますが、特定のインデックスには以前にこの数値にすべての要素を渡す必要があり、これははるかに遅くなければなりません。しかし、この実装からIteratorを取得すると、両方の方法で両方の方法で最適化されたアルゴリズムを繰り返すので、どちらの場合も同じパフォーマンスが得られます。 ResultSetもイテレータですが、同じ方法でdbのクエリのすべての結果を反復し、要素のストアとdbの参加のために構造の責任を隠すことを可能にするjava.utilのインタフェースを実装していません。たとえば、最適化が必要な場合、次の結果の呼び出しごとに新しいResultSet実装のクエリdbを作成することができます。これは、クライアントコードを要素の実現と反復アルゴリズムから切り離すためです。

関連する問題