Employ a Spliterator to iterate through elements in a list.
|
Note: The syntax while( it1.tryAdvance( (n) -> { System.out.println(n); } ) ); is similar to:
boolean while(
}
|
Definition and Usage
The spliterator() method provides a Spliterator for the list. The Spliterator differs significantly from a standard iterator. It aims to divide a collection into smaller segments so that each segment can be processed by a separate thread.
The Spliterator interface includes two crucial methods:
• trySplit() – Produces a new spliterator capable of traversing (usually, roughly half of) the elements accessible to the original spliterator, while the original can traverse the remaining portion.
• tryAdvance(Consumer action) – Progresses to the next available item for the spliterator and attempts to execute an action on it. If there is no next item, it returns false. The action can be specified using a lambda expression.
Syntax
public Spliterator spliterator() |
Technical Details
Returns: |
A Spliterator object. |
Java version: |
1.8+ |