Iterator
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
ListIterator<E> |
An iterator for lists that allows the programmer
to traverse the list in either direction, modify
the list during iteration, and obtain the iterator's
current position in the list. |
PrimitiveIterator<T, T_CONS> |
A base type for primitive specializations of Iterator . |
PrimitiveIterator.OfDouble |
An Iterator specialized for double values. |
PrimitiveIterator.OfInt |
An Iterator specialized for int values. |
PrimitiveIterator.OfLong |
An Iterator specialized for long values. |
Scanner |
A simple text scanner which can parse primitive types and strings using
regular expressions. |
|
An iterator over a collection. Iterator
takes the place of
Enumeration
in the Java Collections Framework. Iterators
differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the
underlying collection during the iteration with well-defined
semantics.
- Method names have been improved.
This interface is a member of the
Java Collections Framework.
Public Method Summary
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.
|
abstract
boolean
|
hasNext()
Returns true if the iteration has more elements.
|
abstract
E
|
next()
Returns the next element in the iteration.
|
void
|
remove()
Removes from the underlying collection the last element returned
by this iterator (optional operation).
|
Public Methods
public
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.
Parameters
action |
The action to be performed for each element |
public
abstract
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
true
if the iteration has more elements
public
abstract
E
next
()
Returns the next element in the iteration.
Returns
- the next element in the iteration
public
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eIterator\u003c/code\u003e interface provides a way to iterate over a collection of elements, replacing the older \u003ccode\u003eEnumeration\u003c/code\u003e interface in Java Collections Framework.\u003c/p\u003e\n"],["\u003cp\u003eIt allows removing elements from the underlying collection during iteration and offers improved method names.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eIterator\u003c/code\u003e includes methods like \u003ccode\u003ehasNext()\u003c/code\u003e, \u003ccode\u003enext()\u003c/code\u003e, \u003ccode\u003eremove()\u003c/code\u003e, and \u003ccode\u003eforEachRemaining()\u003c/code\u003e to facilitate element traversal and manipulation.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eListIterator\u003c/code\u003e, \u003ccode\u003ePrimitiveIterator\u003c/code\u003e, and \u003ccode\u003eScanner\u003c/code\u003e are some of its known indirect subclasses, each offering specialized functionalities.\u003c/p\u003e\n"]]],["The `Iterator` interface allows traversal over a collection, replacing `Enumeration`. Key actions include: `hasNext()`, which checks for more elements; `next()`, which retrieves the next element; `remove()`, which removes the last returned element from the collection (optional); and `forEachRemaining()`, which performs an action on each remaining element. `Iterator` permits element removal during iteration and offers improved method names. Specialized iterators exist for lists and primitive types (double, int, long). `Scanner` is also a subclass.\n"],null,["# Iterator\n\npublic interface **Iterator** \n\n|---|---|---|\n| Known Indirect Subclasses [ListIterator](../../../reference/java/util/ListIterator.html)\\\u003cE\\\u003e, [PrimitiveIterator](../../../reference/java/util/PrimitiveIterator.html)\\\u003cT, T_CONS\\\u003e, [PrimitiveIterator.OfDouble](../../../reference/java/util/PrimitiveIterator.OfDouble.html), [PrimitiveIterator.OfInt](../../../reference/java/util/PrimitiveIterator.OfInt.html), [PrimitiveIterator.OfLong](../../../reference/java/util/PrimitiveIterator.OfLong.html), [Scanner](../../../reference/java/util/Scanner.html) |--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ListIterator](../../../reference/java/util/ListIterator.html)\\\u003cE\\\u003e | An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. | | [PrimitiveIterator](../../../reference/java/util/PrimitiveIterator.html)\\\u003cT, T_CONS\\\u003e | A base type for primitive specializations of `Iterator`. | | [PrimitiveIterator.OfDouble](../../../reference/java/util/PrimitiveIterator.OfDouble.html) | An Iterator specialized for `double` values. | | [PrimitiveIterator.OfInt](../../../reference/java/util/PrimitiveIterator.OfInt.html) | An Iterator specialized for `int` values. | | [PrimitiveIterator.OfLong](../../../reference/java/util/PrimitiveIterator.OfLong.html) | An Iterator specialized for `long` values. | | [Scanner](../../../reference/java/util/Scanner.html) | A simple text scanner which can parse primitive types and strings using regular expressions. | |||\n\nAn iterator over a collection. `Iterator` takes the place of\n[Enumeration](../../../reference/java/util/Enumeration.html) in the Java Collections Framework. Iterators\ndiffer from enumerations in two ways:\n\n- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.\n- Method names have been improved.\n\nThis interface is a member of the\n[Java Collections Framework](../../..//../technotes/guides/collections/index.html). \n\n##### See Also\n\n- [Collection](../../../reference/java/util/Collection.html)\n- [ListIterator](../../../reference/java/util/ListIterator.html)\n- [Iterable](../../../reference/java/lang/Iterable.html) \n\n### Public Method Summary\n\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| void | [forEachRemaining](../../../reference/java/util/Iterator.html#forEachRemaining(java.util.function.Consumer\u003c?%20super%20E\u003e))([Consumer](../../../reference/java/util/function/Consumer.html)\\\u003c? super E\\\u003e action) Performs the given action for each remaining element until all elements have been processed or the action throws an exception. |\n| abstract boolean | [hasNext](../../../reference/java/util/Iterator.html#hasNext())() Returns `true` if the iteration has more elements. |\n| abstract E | [next](../../../reference/java/util/Iterator.html#next())() Returns the next element in the iteration. |\n| void | [remove](../../../reference/java/util/Iterator.html#remove())() Removes from the underlying collection the last element returned by this iterator (optional operation). |\n\nPublic Methods\n--------------\n\n#### public void\n**forEachRemaining**\n([Consumer](../../../reference/java/util/function/Consumer.html)\\\u003c? super E\\\u003e action)\n\nPerforms the given action for each remaining element until all elements\nhave been processed or the action throws an exception. Actions are\nperformed in the order of iteration, if that order is specified.\nExceptions thrown by the action are relayed to the caller. \n\n##### Parameters\n\n| action | The action to be performed for each element |\n|--------|---------------------------------------------|\n\n##### Throws\n\n| [NullPointerException](../../../reference/java/lang/NullPointerException.html) | if the specified action is null |\n|--------------------------------------------------------------------------------|---------------------------------|\n\n#### public abstract boolean\n**hasNext**\n()\n\nReturns `true` if the iteration has more elements.\n(In other words, returns `true` if [next()](../../../reference/java/util/Iterator.html#next()) would\nreturn an element rather than throwing an exception.) \n\n##### Returns\n\n- `true` if the iteration has more elements \n\n#### public abstract E\n**next**\n()\n\nReturns the next element in the iteration. \n\n##### Returns\n\n- the next element in the iteration \n\n##### Throws\n\n| [NoSuchElementException](../../../reference/java/util/NoSuchElementException.html) | if the iteration has no more elements |\n|------------------------------------------------------------------------------------|---------------------------------------|\n\n#### public void\n**remove**\n()\n\nRemoves from the underlying collection the last element returned\nby this iterator (optional operation). This method can be called\nonly once per call to [next()](../../../reference/java/util/Iterator.html#next()). The behavior of an iterator\nis unspecified if the underlying collection is modified while the\niteration is in progress in any way other than by calling this\nmethod. \n\n##### Throws\n\n| [UnsupportedOperationException](../../../reference/java/lang/UnsupportedOperationException.html) | if the `remove` operation is not supported by this iterator |\n| [IllegalStateException](../../../reference/java/lang/IllegalStateException.html) | 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 |\n|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|"]]