Java 为什么我需要迭代器接口,为什么我应该使用它?

Java 为什么我需要迭代器接口,为什么我应该使用它?,java,Java,我是Java新手,所以也许对你们中的一些人来说,我的问题会显得很愚蠢 正如我从一些教程中了解到的,如果我需要在我的自定义对象上创建每个对象,那么该对象必须实现Iterable接口 我的问题是为什么我需要迭代器接口,为什么我应该使用它?正如您所提到的,在foreach循环中使用了Iterable 不是所有的东西都可以在foreach循环中使用,对吗?你认为这会怎么样 for (int a : 10) Java的设计者希望使编译器能够发现这些废话,并将其作为编译器错误报告给您。所以他们想,“什么样

我是Java新手,所以也许对你们中的一些人来说,我的问题会显得很愚蠢

正如我从一些教程中了解到的,如果我需要在我的自定义对象上创建每个对象,那么该对象必须实现Iterable接口


我的问题是为什么我需要迭代器接口,为什么我应该使用它?

正如您所提到的,在foreach循环中使用了
Iterable

不是所有的东西都可以在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();
}
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");
    }
}

正如您所提到的,
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();
}
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");
    }
}
is设计模式允许以某种方式遍历相同对象的集合,这也允许向用户隐藏存储元素和迭代机制的实现。正如您在javadoc中看到的,许多类实现了
Itarable
接口,而不仅仅是集合。在示例中,当
ArrayList
同时给出索引时,它允许您以相同的性能迭代两个
List
实现,但是
LinkedList
对于给定的特定索引,需要将所有元素转到该数字之前,而且速度要慢得多。但是,当您从这个实现中获得迭代器时,您在这两种情况下都会获得相同的性能,因为迭代器算法在这两个列表中以不同的方式进行了优化
ResultSet
也是迭代器,但它没有实现来自
java的接口。util
它允许以相同的方式在数据库中迭代所有查询结果,并隐藏负责元素存储和数据库参与的结构。在示例中,当您需要一些优化时,您可能会根据下一个结果调用或您想要的任何东西来创建新的ResultSet实现查询db,因为它还将客户端代码与元素存储实现和迭代算法解耦。

is设计模式允许以某种方式遍历相同对象的集合,这还允许对用户隐藏存储元素和迭代机制的实现。正如您在javadoc中看到的,许多类实现了
Itarable
接口,而不仅仅是集合。在示例中,当
ArrayList
同时给出索引时,它允许您以相同的性能迭代两个
List
实现,但是
LinkedList
对于给定的特定索引,需要将所有元素转到该数字之前,而且速度要慢得多。但是,当您从这个实现中获得迭代器时,您在这两种情况下都会获得相同的性能,因为迭代器算法在这两个列表中以不同的方式进行了优化
ResultSet
也是迭代器,但它没有实现来自
java的接口。util
它允许以相同的方式在数据库中迭代所有查询结果,并隐藏负责元素存储和数据库参与的结构。在这个示例中,当您需要一些优化时,您可以让新的ResultSet实现根据下一个结果调用查询db,或者做任何您想要的事情,因为它还将客户机代码与元素存储实现和迭代算法解耦