author | henryjen |
Tue, 22 Oct 2013 15:12:22 -0700 | |
changeset 21339 | 20e8b81964d5 |
parent 16929 | c984ae5655cb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16929 | 2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
16929 | 28 |
import java.util.function.Consumer; |
29 |
||
2 | 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 |
* |
|
4977
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
2
diff
changeset
|
46 |
* @param <E> the type of elements returned by this iterator |
81902a400bbf
6929382: Various core classes in util and elsewhere are missing @param <T> tags
darcy
parents:
2
diff
changeset
|
47 |
* |
2 | 48 |
* @author Josh Bloch |
49 |
* @see Collection |
|
50 |
* @see ListIterator |
|
51 |
* @see Iterable |
|
52 |
* @since 1.2 |
|
53 |
*/ |
|
54 |
public interface Iterator<E> { |
|
55 |
/** |
|
56 |
* Returns {@code true} if the iteration has more elements. |
|
57 |
* (In other words, returns {@code true} if {@link #next} would |
|
58 |
* return an element rather than throwing an exception.) |
|
59 |
* |
|
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 |
* |
|
16929 | 80 |
* @implSpec |
81 |
* The default implementation throws an instance of |
|
82 |
* {@link UnsupportedOperationException} and performs no other action. |
|
83 |
* |
|
2 | 84 |
* @throws UnsupportedOperationException if the {@code remove} |
85 |
* operation is not supported by this iterator |
|
86 |
* |
|
87 |
* @throws IllegalStateException if the {@code next} method has not |
|
88 |
* yet been called, or the {@code remove} method has already |
|
89 |
* been called after the last call to the {@code next} |
|
90 |
* method |
|
91 |
*/ |
|
16929 | 92 |
default void remove() { |
93 |
throw new UnsupportedOperationException("remove"); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
21339 | 97 |
* Performs the given action for each remaining element until all elements |
98 |
* have been processed or the action throws an exception. Actions are |
|
99 |
* performed in the order of iteration, if that order is specified. |
|
100 |
* Exceptions thrown by the action are relayed to the caller. |
|
16929 | 101 |
* |
102 |
* @implSpec |
|
103 |
* <p>The default implementation behaves as if: |
|
104 |
* <pre>{@code |
|
105 |
* while (hasNext()) |
|
106 |
* action.accept(next()); |
|
107 |
* }</pre> |
|
108 |
* |
|
109 |
* @param action The action to be performed for each element |
|
110 |
* @throws NullPointerException if the specified action is null |
|
111 |
* @since 1.8 |
|
112 |
*/ |
|
113 |
default void forEachRemaining(Consumer<? super E> action) { |
|
114 |
Objects.requireNonNull(action); |
|
115 |
while (hasNext()) |
|
116 |
action.accept(next()); |
|
117 |
} |
|
2 | 118 |
} |