前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >迭代器 Iterator 接口源码分析

迭代器 Iterator 接口源码分析

作者头像
收心
发布2022-01-17 15:08:47
2840
发布2022-01-17 15:08:47
举报
文章被收录于专栏:Java实战博客

遍历 List 集合

代码语言:javascript
复制
        List list =new ArrayList();
        list.add("list4");
        list.add("list2");
        list.add("list3");
        Iterator iterator=list.iterator();
        for (iterator.hasNext()){
            System.out.println(iterator.next());
        }

等价于
        for (Iterator iterator = list.iterator(); iterator.hasNext();){
            System.out.println(iterator.next());
        }

遍历 Set 集合

代码语言:javascript
复制
        Set set=new HashSet();
        set.add("set");
        set.add("set2");
        Iterator iterator1=set.iterator();
        while(iterator1.hasNext()){
             System.out.println(iterator1.next());
        }

遍历 Map 集合

代码语言:javascript
复制
        Map map=new HashMap();
        map.put("a","map");
        map.put("b","map2");

        //先将map中的键存入Set,再使用迭代器遍历
        Set<String> set1=map.keySet();

        Iterator iterator2=set1.iterator();
        while (iterator2.hasNext()){
            System.out.println(map.get(iterator2.next()));
        }

Iterator<E>

代码语言:javascript
复制
/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */

package java.util;

import java.util.function.Consumer;

/**
 * An iterator over a collection.  {@code Iterator} takes the place of
 * {@link Enumeration} in the Java Collections Framework.  Iterators
 * differ from enumerations in two ways:
 *
 * <ul>
 *      <li> Iterators allow the caller to remove elements from 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>.
 *
 * @param <E> the type of elements returned by this iterator
 *
 * @author  Josh Bloch
 * @see Collection
 * @see ListIterator
 * @see Iterable
 * @since 1.2
 */
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
     */
    //返回迭代中的下一个元素,如果没有元素了,就抛出NoSuchElementExceptio异常
    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");
    }

    /**
     * Performs the given action for each remaining element until all elements
     * have been processed or the action throws an exception.  Actions are
     * performed in the order of iteration, if that order is specified.
     * Exceptions thrown by the action are relayed to the caller.
     *
     * @implSpec
     * <p>The default implementation behaves as if:
     * <pre>{@code
     *     while (hasNext())
     *         action.accept(next());
     * }</pre>
     *
     * @param action The action to be performed for each element
     * @throws NullPointerException if the specified action is null
     * @since 1.8
     */
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

特殊说明: 解决问题的光鲜,藏着磕Bug的痛苦。 万物皆入轮回,谁也躲不掉! 以上文章,均是我实际操作,写出来的笔记资料,不会出现全文盗用别人文章!烦请各位,请勿直接盗用!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 遍历 List 集合
  • 遍历 Set 集合
  • 遍历 Map 集合
  • Iterator<E>
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档