151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
23984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.util;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
283984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamathimport java.util.function.Consumer;
293984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * An iterator over a collection.  {@code Iterator} takes the place of
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Enumeration} in the Java Collections Framework.  Iterators
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * differ from enumerations in two ways:
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul>
3651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *      <li> Iterators allow the caller to remove elements from the
3751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *           underlying collection during the iteration with well-defined
3851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *           semantics.
3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *      <li> Method names have been improved.
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul>
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This interface is a member of the
43d2449bb576ad1e3a3877364e5e1ae28625f69e35Yi Kong * <a href="{@docRoot}openjdk-redirect.html?v=8&path=/technotes/guides/collections/index.html">
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Collections Framework</a>.
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param <E> the type of elements returned by this iterator
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author  Josh Bloch
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Collection
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see ListIterator
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Iterable
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic interface Iterator<E> {
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns {@code true} if the iteration has more elements.
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * (In other words, returns {@code true} if {@link #next} would
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * return an element rather than throwing an exception.)
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return {@code true} if the iteration has more elements
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    boolean hasNext();
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the next element in the iteration.
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return the next element in the iteration
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws NoSuchElementException if the iteration has no more elements
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    E next();
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Removes from the underlying collection the last element returned
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * by this iterator (optional operation).  This method can be called
7551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * only once per call to {@link #next}.  The behavior of an iterator
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * is unspecified if the underlying collection is modified while the
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * iteration is in progress in any way other than by calling this
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method.
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
803984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * @implSpec
813984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * The default implementation throws an instance of
823984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * {@link UnsupportedOperationException} and performs no other action.
833984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     *
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws UnsupportedOperationException if the {@code remove}
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         operation is not supported by this iterator
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalStateException if the {@code next} method has not
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         yet been called, or the {@code remove} method has already
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         been called after the last call to the {@code next}
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         method
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
923984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath    default void remove() {
933984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath        throw new UnsupportedOperationException("remove");
943984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath    }
953984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath
963984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath    /**
973984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * Performs the given action for each remaining element until all elements
983984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * have been processed or the action throws an exception.  Actions are
993984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * performed in the order of iteration, if that order is specified.
1003984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * Exceptions thrown by the action are relayed to the caller.
1013984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     *
1023984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * @implSpec
1033984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * <p>The default implementation behaves as if:
1043984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * <pre>{@code
1053984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     *     while (hasNext())
1063984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     *         action.accept(next());
1073984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * }</pre>
1083984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     *
1093984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * @param action The action to be performed for each element
1103984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * @throws NullPointerException if the specified action is null
1113984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     * @since 1.8
1123984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath     */
1133984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath    default void forEachRemaining(Consumer<? super E> action) {
1143984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath        Objects.requireNonNull(action);
1153984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath        while (hasNext())
1163984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath            action.accept(next());
1173984cdba314a0f7b0587000dec99ff26fd9bcbb5Narayan Kamath    }
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
119