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 /**
29 * An iterator over a collection. {@code Iterator} takes the place of
30 * {@link Enumeration} in the Java Collections Framework. Iterators
31 * differ from enumerations in two ways:
32 *
33 * <ul>
34 * <li> Iterators allow the caller to remove elements from the
35 * underlying collection during the iteration with well-defined
36 * semantics.
37 * <li> Method names have been improved.
38 * </ul>
39 *
40 * <p>This interface is a member of the
41 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
42 * Java Collections Framework</a>.
43 *
44 * @param <E> the type of elements returned by this iterator
45 *
46 * @author Josh Bloch
47 * @see Collection
58 * @return {@code true} if the iteration has more elements
59 */
60 boolean hasNext();
61
62 /**
63 * Returns the next element in the iteration.
64 *
65 * @return the next element in the iteration
66 * @throws NoSuchElementException if the iteration has no more elements
67 */
68 E next();
69
70 /**
71 * Removes from the underlying collection the last element returned
72 * by this iterator (optional operation). This method can be called
73 * only once per call to {@link #next}. The behavior of an iterator
74 * is unspecified if the underlying collection is modified while the
75 * iteration is in progress in any way other than by calling this
76 * method.
77 *
78 * @throws UnsupportedOperationException if the {@code remove}
79 * operation is not supported by this iterator
80 *
81 * @throws IllegalStateException if the {@code next} method has not
82 * yet been called, or the {@code remove} method has already
83 * been called after the last call to the {@code next}
84 * method
85 */
86 void remove();
87 }
|
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.Block;
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}/../technotes/guides/collections/index.html">
44 * Java Collections Framework</a>.
45 *
46 * @param <E> the type of elements returned by this iterator
47 *
48 * @author Josh Bloch
49 * @see Collection
60 * @return {@code true} if the iteration has more elements
61 */
62 boolean hasNext();
63
64 /**
65 * Returns the next element in the iteration.
66 *
67 * @return the next element in the iteration
68 * @throws NoSuchElementException if the iteration has no more elements
69 */
70 E next();
71
72 /**
73 * Removes from the underlying collection the last element returned
74 * by this iterator (optional operation). This method can be called
75 * only once per call to {@link #next}. The behavior of an iterator
76 * is unspecified if the underlying collection is modified while the
77 * iteration is in progress in any way other than by calling this
78 * method.
79 *
80 * This default implementation does not have knowledge of the
81 * synchronization protocol used by the implementing Collection. It is
82 * the caller's responsibility to ensure that usage follows the correct
83 * synchronization protocol for the implementing Collection.
84 *
85 * This default implementation may be overridden by implementing
86 * Collections to provide optimal performance or to include this method
87 * within their synchronization protocol.
88 *
89 * @throws UnsupportedOperationException if the {@code remove}
90 * operation is not supported by this iterator
91 *
92 * @throws IllegalStateException if the {@code next} method has not
93 * yet been called, or the {@code remove} method has already
94 * been called after the last call to the {@code next}
95 * method
96 */
97 public default void remove() {
98 throw new UnsupportedOperationException("remove");
99 }
100
101 /**
102 * Execute the specified Block for each element. After successful
103 * execution subsequent calls to {@link #hasNext} will return {@code false}.
104 *
105 * This default implementation does not have knowledge of the
106 * synchronization protocol used by the implementing Collection. It is
107 * the caller's responsibility to ensure that usage follows the correct
108 * synchronization protocol for the implementing Collection.
109 *
110 * This default implementation may be overridden by implementing
111 * Collections to provide optimal performance or to include this method
112 * within their synchronization protocol.
113 *
114 * @param block The Block to which elements will be provided
115 * @throws NullPointerException if the specified block is null
116 * @since 1.8
117 */
118 public default void forEach(Block<? super E> block) {
119 Objects.requireNonNull(block);
120 while (hasNext()) {
121 block.accept(next());
122 }
123 }
124 }
|