首页 > 编程语言 > 详细

Java--Iterable和Iterator

时间:2020-05-10 15:56:02      阅读:43      评论:0      收藏:0      [点我收藏+]

Iterable

Iterable接口来自java.lang包,实现了Iterable接口的类可以使用for each去遍历。

Iterable接口通过iterator()方法返回一个Iterator实例

package java.lang;

/**
 * Implementing this interface allows an object to be the target of
 * the "for-each loop" statement.
 */
public interface Iterable<T> {
    /**
     * Returns an iterator over elements of type
     *
     * @return an Iterator.
     */
    Iterator<T> iterator();

    /**
     * @param action The action to be performed for each element
     * @throws NullPointerException if the specified action is null
     * @since 1.8
     */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

}

Java--Iterable和Iterator

原文:https://www.cnblogs.com/swifthao/p/12863355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!