Flash cards
Review the key moves
What is the main idea behind Java ArrayList spliterator() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Put the learning moves in the order that makes the concept easiest to apply.
❮ ArrayList Methods
SpliteratorNote
The syntax while( it1.tryAdvance( (n) -> { System.out.println(n); } ) ); is equivalent to:
boolean x = it1.tryAdvance( (n) -> { System.out.println(n); });
while(x) {
x = it1.tryAdvance( (n) -> { System.out.println(n); });
}Definition and Usage
The spliterator() method returns a Spliterator for the list.
A spliterator is a special type of iterator. To learn how to use iterators, see our Java Iterator tutorial .
The Spliterator is considerably different from an ordinary iterator. The purpose of a spliterator is to separate a collection into smaller pieces so that each piece can be processed by a separate thread. The Spliterator interface has two important methods:
- trySplit() - Returns a new spliterator which can iterate through (usually and approximately) half of the elements that the original spliterator has access to, while the original spliterator can iterate through the remaining half.
- tryAdvance(Consumer action ) - Moves to the next item that is available to the spliterator and tries to perform an action on it. If there is no next item then it returns false . The action can be defined by a lambda expression.
Syntax
public Spliterator spliterator()Technical Details
| Returns: | A Spliterator object. |
|---|---|
| Java version: | 1.8+ |