APIdia Launch Partner
Top Description Methods
java.util

public Interface Iterator<E>

Known Direct Subinterfaces
java.util.ListIterator, java.util.PrimitiveIterator
Known Direct Implementers
java.util.JumboEnumSet.EnumSetIterator, java.util.LazyCollections.AbstractLazyMap.LazyMapEntrySet.LazyMapIterator, java.util.LazyCollections.LazySet.LazySetIterator, java.util.LinkedHashMap.LinkedKeyIterator, java.util.LinkedHashMap.LinkedValueIterator, java.util.LinkedHashMap.LinkedEntryIterator, java.util.LinkedList.DescendingIterator, java.util.PriorityQueue.Itr, java.util.RegularEnumSet.EnumSetIterator, java.util.ReverseOrderListView.DescendingIterator, java.util.Scanner, java.util.ServiceLoader.LayerLookupIterator, java.util.ServiceLoader.ModuleServicesLookupIterator, java.util.ServiceLoader.LazyClassPathLookupIterator, java.util.TreeMap.PrivateEntryIterator, java.util.TreeMap.NavigableSubMap.SubMapIterator, java.util.Vector.Itr, java.util.WeakHashMap.HashIterator, java.lang.invoke.AbstractConstantGroup.AsIterator, java.lang.reflect.AccessFlag.AccessFlagSet.AccessFlagIterator, java.nio.file.FileTreeIterator, java.util.concurrent.ArrayBlockingQueue.Itr, java.util.concurrent.ConcurrentHashMap.KeyIterator, java.util.concurrent.ConcurrentHashMap.ValueIterator, java.util.concurrent.ConcurrentHashMap.EntryIterator, java.util.concurrent.ConcurrentLinkedDeque.AbstractItr, java.util.concurrent.ConcurrentLinkedQueue.Itr, java.util.concurrent.ConcurrentSkipListMap.Iter, java.util.concurrent.ConcurrentSkipListMap.SubMap.SubMapIter, java.util.concurrent.CopyOnWriteArrayList.Reversed.DescendingIterator, java.util.concurrent.DelayQueue.Itr, java.util.concurrent.LinkedBlockingDeque.AbstractItr, java.util.concurrent.LinkedBlockingQueue.Itr, java.util.concurrent.LinkedTransferQueue.Itr, java.util.concurrent.PriorityBlockingQueue.Itr, java.util.concurrent.ScheduledThreadPoolExecutor.DelayedWorkQueue.Itr, java.util.zip.ZipFile.ZipEntryIterator, sun.net.www.HeaderParser.ParserIterator, sun.net.www.MessageHeader.HeaderIterator, sun.nio.fs.UnixDirectoryStream.UnixDirectoryIterator, sun.nio.fs.UnixFileSystem.FileStoreIterator, sun.security.jca.ProviderList.ServiceIterator, jdk.internal.icu.impl.Trie2.Trie2Iterator, jdk.internal.icu.util.CodePointMap.RangeIterator, sun.net.ftp.impl.FtpClient.FtpFileIterator, java.util.AbstractList.Itr, java.util.AbstractMap.KeyIterator, java.util.AbstractMap.ValueIterator, java.util.ArrayDeque.DeqIterator, java.util.ArrayList.Itr, java.util.Arrays.ArrayItr, java.util.Collections.EmptyIterator, java.util.EnumMap.EnumMapIterator, java.util.HashMap.KeyIterator, java.util.HashMap.ValueIterator, java.util.HashMap.EntryIterator, java.util.Hashtable.Enumerator, java.util.IdentityHashMap.IdentityHashMapIterator, java.util.ImmutableCollections.SetN.SetNIterator, java.util.ImmutableCollections.MapN.MapNIterator
Type Parameters
<E>
the type of elements returned by this iterator
Imports
java.util.function.Consumer

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:

This interface is a member of the Java Collections Framework.

API Note

An Enumeration can be converted into an Iterator by using the Enumeration#asIterator method.

Author
Josh Bloch
Since
1.2
See Also
Collection, ListIterator, Iterable

Method Summary

Modifier and TypeMethod and Description
public default void
forEachRemaining(Consumer<? super E>
The action to be performed for each element
action
)

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

public boolean

Returns:

true if the iteration has more elements
hasNext
()

Returns true if the iteration has more elements.

public E

Returns:

the next element in the iteration
next
()

Returns the next element in the iteration.

public default void
remove()

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Detail

forEachRemainingback to summary
public default void forEachRemaining(Consumer<? super E> action)

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.

The behavior of an iterator is unspecified if the action modifies the collection in any way (even by calling the remove method or other mutator methods of Iterator subtypes), unless an overriding class has specified a concurrent modification policy.

Subsequent behavior of an iterator is unspecified if the action throws an exception.

Implementation Specification

The default implementation behaves as if:

while (hasNext())
        action.accept(next());
Parameters
action:Consumer<? super E>

The action to be performed for each element

Exceptions
NullPointerException:
if the specified action is null
Since
1.8
hasNextback to summary
public boolean hasNext()

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public E next()

Returns the next element in the iteration.

Returns:E

the next element in the iteration

Exceptions
NoSuchElementException:
if the iteration has no more elements
removeback to summary
public default void remove()

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to 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, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

Implementation Specification

The default implementation throws an instance of UnsupportedOperationException and performs no other action.

Exceptions
UnsupportedOperationException:
if the remove operation is not supported by this iterator
IllegalStateException:
if the next method has not yet been called, or the remove method has already been called after the last call to the next method