Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/util/Iterator#pubmethods)

# Iterator

` public interface Iterator `   
` `

|---|
| java.util.Iterator\<E\> |

Known indirect subclasses [CursorJoiner](https://developer.android.com/reference/android/database/CursorJoiner), [EntityIterator](https://developer.android.com/reference/android/content/EntityIterator), [ListIterator](https://developer.android.com/reference/java/util/ListIterator)\<E\>, [PathIterator](https://developer.android.com/reference/android/graphics/PathIterator), [PrimitiveIterator](https://developer.android.com/reference/java/util/PrimitiveIterator)\<T, T_CONS\>, [PrimitiveIterator.OfDouble](https://developer.android.com/reference/java/util/PrimitiveIterator.OfDouble), [PrimitiveIterator.OfInt](https://developer.android.com/reference/java/util/PrimitiveIterator.OfInt), [PrimitiveIterator.OfLong](https://developer.android.com/reference/java/util/PrimitiveIterator.OfLong), [Scanner](https://developer.android.com/reference/java/util/Scanner), [TextUtils.SimpleStringSplitter](https://developer.android.com/reference/android/text/TextUtils.SimpleStringSplitter) [CursorJoiner](https://developer.android.com/reference/android/database/CursorJoiner) Does a join on two cursors using the specified columns. [EntityIterator](https://developer.android.com/reference/android/content/EntityIterator) A specialization of `https://developer.android.com/reference/java/util/Iterator` that allows iterating over a collection of `https://developer.android.com/reference/android/content/Entity` objects. [ListIterator](https://developer.android.com/reference/java/util/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. [PathIterator](https://developer.android.com/reference/android/graphics/PathIterator) `PathIterator` can be used to query a given `https://developer.android.com/reference/android/graphics/Path` object, to discover its operations and point values. [PrimitiveIterator](https://developer.android.com/reference/java/util/PrimitiveIterator)\<T, T_CONS\> A base type for primitive specializations of `Iterator`. [PrimitiveIterator.OfDouble](https://developer.android.com/reference/java/util/PrimitiveIterator.OfDouble) An Iterator specialized for `double` values. [PrimitiveIterator.OfInt](https://developer.android.com/reference/java/util/PrimitiveIterator.OfInt) An Iterator specialized for `int` values. [PrimitiveIterator.OfLong](https://developer.android.com/reference/java/util/PrimitiveIterator.OfLong) An Iterator specialized for `long` values. [Scanner](https://developer.android.com/reference/java/util/Scanner) A simple text scanner which can parse primitive types and strings using regular expressions. [TextUtils.SimpleStringSplitter](https://developer.android.com/reference/android/text/TextUtils.SimpleStringSplitter) A simple string splitter.   

*** ** * ** ***

An iterator over a collection. `Iterator` takes the place of `https://developer.android.com/reference/java/util/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](https://reference/java/util/package-summary.html#CollectionsFramework).
**See also:**

- `https://developer.android.com/reference/java/util/Collection`
- `https://developer.android.com/reference/java/util/ListIterator`
- `https://developer.android.com/reference/java/lang/Iterable`

## Summary

| ### Public methods ||
|---|---|
| ` default void` | ` https://developer.android.com/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer<?%20super%20E>)(https://developer.android.com/reference/java/util/function/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` | ` https://developer.android.com/reference/java/util/Iterator#hasNext()() ` Returns `true` if the iteration has more elements. |
| ` abstract E` | ` https://developer.android.com/reference/java/util/Iterator#next()() ` Returns the next element in the iteration. |
| ` default void` | ` https://developer.android.com/reference/java/util/Iterator#remove()() ` Removes from the underlying collection the last element returned by this iterator (optional operation). |

## Public methods

### forEachRemaining

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

The behavior of an iterator is unspecified if the action modifies the collection in any way (even by calling the `https://developer.android.com/reference/java/util/Iterator#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.

<br />

##### Implementation Requirements:

- The default implementation behaves as if:

      while (hasNext())
              action.accept(next());

| Parameters ||
|---|---|
| `action` | `Consumer`: The action to be performed for each element <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the specified action is null |

### hasNext

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean hasNext ()
```

Returns `true` if the iteration has more elements. (In other words, returns `true` if `https://developer.android.com/reference/java/util/Iterator#next()` would return an element rather than throwing an exception.)

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the iteration has more elements <br /> |

### next

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract E next ()
```

Returns the next element in the iteration.

<br />

| Returns ||
|---|---|
| `E` | the next element in the iteration <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/NoSuchElementException` | if the iteration has no more elements |

### remove

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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 `https://developer.android.com/reference/java/util/Iterator#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 `https://developer.android.com/reference/java/util/Iterator#forEachRemaining(java.util.function.Consumer<?%20super%20E>)` method.

<br />

##### Implementation Requirements:

- The default implementation throws an instance of `https://developer.android.com/reference/java/lang/UnsupportedOperationException` and performs no other action.

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/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 |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | if the `remove` operation is not supported by this iterator |