1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Consumer;
  29 
  30 /**
  31  * An iterator over a collection.  {@code Iterator} takes the place of
  32  * {@link Enumeration} in the Java Collections Framework.  Iterators
  33  * differ from enumerations in two ways:
  34  *
  35  * <ul>
  36  *      <li> Iterators allow the caller to remove elements from the
  37  *           underlying collection during the iteration with well-defined
  38  *           semantics.
  39  *      <li> Method names have been improved.
  40  * </ul>
  41  *
  42  * <p>This interface is a member of the
  43  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  44  * Java Collections Framework</a>.
  45  *
  46  * @apiNote
  47  * An {@link Enumeration} can be converted into an {@code Iterator} by
  48  * using the {@link Enumeration#asIterator} method.
  49  *
  50  * @param <E> the type of elements returned by this iterator
  51  *
  52  * @author  Josh Bloch
  53  * @see Collection
  54  * @see ListIterator
  55  * @see Iterable
  56  * @since 1.2
  57  */
  58 public interface Iterator<E> {
  59     /**
  60      * Returns {@code true} if the iteration has more elements.
  61      * (In other words, returns {@code true} if {@link #next} would
  62      * return an element rather than throwing an exception.)
  63      *
  64      * @return {@code true} if the iteration has more elements
  65      */
  66     boolean hasNext();
  67 
  68     /**
  69      * Returns the next element in the iteration.
  70      *
  71      * @return the next element in the iteration
  72      * @throws NoSuchElementException if the iteration has no more elements
  73      */
  74     E next();
  75 
  76     /**
  77      * Removes from the underlying collection the last element returned
  78      * by this iterator (optional operation).  This method can be called
  79      * only once per call to {@link #next}.
  80      * <p>
  81      * The behavior of an iterator is unspecified if the underlying collection
  82      * is modified while the iteration is in progress in any way other than by
  83      * calling this method, unless an overriding class has specified a
  84      * concurrent modification policy.
  85      * <p>
  86      * The behavior of an iterator is unspecified if this method is called
  87      * after a call to the {@link #forEachRemaining forEachRemaining} method.
  88      *
  89      * @implSpec
  90      * The default implementation throws an instance of
  91      * {@link UnsupportedOperationException} and performs no other action.
  92      *
  93      * @throws UnsupportedOperationException if the {@code remove}
  94      *         operation is not supported by this iterator
  95      *
  96      * @throws IllegalStateException if the {@code next} method has not
  97      *         yet been called, or the {@code remove} method has already
  98      *         been called after the last call to the {@code next}
  99      *         method
 100      */
 101     default void remove() {
 102         throw new UnsupportedOperationException("remove");
 103     }
 104 
 105     /**
 106      * Performs the given action for each remaining element until all elements
 107      * have been processed or the action throws an exception.  Actions are
 108      * performed in the order of iteration, if that order is specified.
 109      * Exceptions thrown by the action are relayed to the caller.
 110      * <p>
 111      * The behavior of an iterator is unspecified if the action modifies the
 112      * collection in any way (even by calling the {@link #remove remove} method
 113      * or other mutator methods of {@code Iterator} subtypes),
 114      * unless an overriding class has specified a concurrent modification policy.
 115      * <p>
 116      * Subsequent behavior of an iterator is unspecified if the action throws an
 117      * exception.
 118      *
 119      * @implSpec
 120      * <p>The default implementation behaves as if:
 121      * <pre>{@code
 122      *     while (hasNext())
 123      *         action.accept(next());
 124      * }</pre>
 125      *
 126      * @param action The action to be performed for each element
 127      * @throws NullPointerException if the specified action is null
 128      * @since 1.8
 129      */
 130     default void forEachRemaining(Consumer<? super E> action) {
 131         Objects.requireNonNull(action);
 132         while (hasNext())
 133             action.accept(next());
 134     }
 135 }