PrimitiveIterator
public
interface
PrimitiveIterator
implements
Iterator<T>
java.util.PrimitiveIterator<T, T_CONS>
|
Known indirect subclasses
|
A base type for primitive specializations of Iterator
. Specialized
subtypes are provided for int
, long
, and
double
values.
The specialized subtype default implementations of Iterator.next
and Iterator.forEachRemaining(java.util.function.Consumer)
box
primitive values to instances of their corresponding wrapper class. Such
boxing may offset any advantages gained when using the primitive
specializations. To avoid boxing, the corresponding primitive-based methods
should be used. For example, PrimitiveIterator.OfInt.nextInt()
and
PrimitiveIterator.OfInt.forEachRemaining(java.util.function.IntConsumer)
should be used in preference to PrimitiveIterator.OfInt.next()
and
PrimitiveIterator.OfInt.forEachRemaining(java.util.function.Consumer)
.
Iteration of primitive values using boxing-based methods
next()
and
forEachRemaining()
,
does not affect the order in which the values, transformed to boxed values,
are encountered.
Summary
Nested classes |
interface |
PrimitiveIterator.OfDouble
An Iterator specialized for double values.
|
interface |
PrimitiveIterator.OfInt
An Iterator specialized for int values.
|
interface |
PrimitiveIterator.OfLong
An Iterator specialized for long values.
|
Public methods |
abstract
void
|
forEachRemaining(T_CONS action)
Performs the given action for each remaining element until all elements
have been processed or the action throws an exception.
|
Inherited methods |
From interface
java.util.Iterator
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.
|
abstract
boolean
|
hasNext()
Returns true if the iteration has more elements.
|
abstract
T
|
next()
Returns the next element in the iteration.
|
default
void
|
remove()
Removes from the underlying collection the last element returned
by this iterator (optional operation).
|
|
Public methods
forEachRemaining
public abstract void forEachRemaining (T_CONS 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
source of elements 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.
Parameters |
action |
T_CONS : The action to be performed for each element |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# PrimitiveIterator\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Nested Classes](#nestedclasses) \\| [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nPrimitiveIterator\n=================\n\n\n`\npublic\n\n\ninterface\nPrimitiveIterator\n`\n\n\n`\n\n\nimplements\n\n`[Iterator](/reference/java/util/Iterator)`\u003cT\u003e\n\n\n`\n\n|------------------------------------------|\n| java.util.PrimitiveIterator\\\u003cT, T_CONS\\\u003e |\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known indirect subclasses [PrimitiveIterator.OfDouble](/reference/java/util/PrimitiveIterator.OfDouble), [PrimitiveIterator.OfInt](/reference/java/util/PrimitiveIterator.OfInt), [PrimitiveIterator.OfLong](/reference/java/util/PrimitiveIterator.OfLong) |-------------------------------------------------------------------------------|----------------------------------------------| | [PrimitiveIterator.OfDouble](/reference/java/util/PrimitiveIterator.OfDouble) | An Iterator specialized for `double` values. | | [PrimitiveIterator.OfInt](/reference/java/util/PrimitiveIterator.OfInt) | An Iterator specialized for `int` values. | | [PrimitiveIterator.OfLong](/reference/java/util/PrimitiveIterator.OfLong) | An Iterator specialized for `long` values. | |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA base type for primitive specializations of `Iterator`. Specialized\nsubtypes are provided for [int](/reference/java/util/PrimitiveIterator.OfInt), [long](/reference/java/util/PrimitiveIterator.OfLong), and\n[double](/reference/java/util/PrimitiveIterator.OfDouble) values.\n\nThe specialized subtype default implementations of [Iterator.next](/reference/java/util/Iterator#next())\nand [Iterator.forEachRemaining(java.util.function.Consumer)](/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer\u003c?%20super%20E\u003e)) box\nprimitive values to instances of their corresponding wrapper class. Such\nboxing may offset any advantages gained when using the primitive\nspecializations. To avoid boxing, the corresponding primitive-based methods\nshould be used. For example, [PrimitiveIterator.OfInt.nextInt()](/reference/java/util/PrimitiveIterator.OfInt#nextInt()) and\n[PrimitiveIterator.OfInt.forEachRemaining(java.util.function.IntConsumer)](/reference/java/util/PrimitiveIterator.OfInt#forEachRemaining(java.util.function.IntConsumer))\nshould be used in preference to [PrimitiveIterator.OfInt.next()](/reference/java/util/PrimitiveIterator.OfInt#next()) and\n[PrimitiveIterator.OfInt.forEachRemaining(java.util.function.Consumer)](/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer\u003c?%20super%20E\u003e)).\n\nIteration of primitive values using boxing-based methods\n[next()](/reference/java/util/Iterator#next()) and\n[forEachRemaining()](/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer\u003c?%20super%20E\u003e)),\ndoes not affect the order in which the values, transformed to boxed values,\nare encountered.\n\nSummary\n-------\n\n| ### Nested classes ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------|\n| ` interface` | [PrimitiveIterator.OfDouble](/reference/java/util/PrimitiveIterator.OfDouble) An Iterator specialized for `double` values. |\n| ` interface` | [PrimitiveIterator.OfInt](/reference/java/util/PrimitiveIterator.OfInt) An Iterator specialized for `int` values. |\n| ` interface` | [PrimitiveIterator.OfLong](/reference/java/util/PrimitiveIterator.OfLong) An Iterator specialized for `long` values. |\n\n| ### Public methods ||\n|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract void` | ` `[forEachRemaining](/reference/java/util/PrimitiveIterator#forEachRemaining(T_CONS))`(T_CONS action) ` Performs the given action for each remaining element until all elements have been processed or the action throws an exception. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` `[java.util.Iterator](/reference/java/util/Iterator)` ` |---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` default void` | ` `[forEachRemaining](/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer\u003c?%20super%20E\u003e))`(`[Consumer](/reference/java/util/function/Consumer)`\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. | | ` abstract boolean` | ` `[hasNext](/reference/java/util/Iterator#hasNext())`() ` Returns `true` if the iteration has more elements. | | ` abstract T` | ` `[next](/reference/java/util/Iterator#next())`() ` Returns the next element in the iteration. | | ` default void` | ` `[remove](/reference/java/util/Iterator#remove())`() ` Removes from the underlying collection the last element returned by this iterator (optional operation). | ||\n\nPublic methods\n--------------\n\n### forEachRemaining\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void forEachRemaining (T_CONS action)\n```\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\nThe behavior of an iterator is unspecified if the action modifies the\nsource of elements in any way (even by calling the [remove](/reference/java/util/Iterator#remove())\nmethod or other mutator methods of `Iterator` subtypes),\nunless an overriding class has specified a concurrent modification policy.\n\n\nSubsequent behavior of an iterator is unspecified if the action throws an\nexception.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|----------|--------------------------------------------------------------|\n| `action` | `T_CONS`: The action to be performed for each element \u003cbr /\u003e |\n\n| Throws ||\n|-------------------------------------------------------------------|---------------------------------|\n| [NullPointerException](/reference/java/lang/NullPointerException) | if the specified action is null |"]]