Dart HasNextIterator
last modified April 4, 2025
In Dart, HasNextIterator is a wrapper around another iterator that provides lookahead capability. It allows checking if there are more elements without consuming them.
This is useful when you need to peek at the next element in the iteration sequence. The class is part of the dart:collection library and extends the Iterator interface.
Basic Usage of HasNextIterator
The simplest way to use HasNextIterator is to wrap an existing iterator.
import 'dart:collection'; void main() { var numbers = [1, 2, 3].iterator; var iterator = HasNextIterator(numbers); while (iterator.hasNext) { print(iterator.moveNext() ? iterator.current : 'No more elements'); } }
We create a HasNextIterator from a List iterator. The hasNext property checks if more elements exist. moveNext advances the iterator and returns true if successful.
$ dart main.dart 1 2 3
Peeking at Next Element
HasNextIterator allows peeking at the next element without consuming it.
import 'dart:collection'; void main() { var colors = ['red', 'green', 'blue'].iterator; var iterator = HasNextIterator(colors); while (iterator.hasNext) { print('Next color will be: ${iterator.peek}'); iterator.moveNext(); print('Current color: ${iterator.current}'); } }
The peek property returns the next element without advancing the iterator. This is useful for lookahead operations. Note that peek throws if no more elements exist.
$ dart main.dart Next color will be: red Current color: red Next color will be: green Current color: green Next color will be: blue Current color: blue
Processing Pairs of Elements
HasNextIterator is helpful when you need to process elements in pairs.
import 'dart:collection'; void main() { var values = [10, 20, 30, 40].iterator; var iterator = HasNextIterator(values); while (iterator.hasNext) { iterator.moveNext(); var current = iterator.current; if (iterator.hasNext) { var next = iterator.peek; print('Pair: ($current, $next)'); } else { print('Single: $current'); } } }
This example processes adjacent pairs of elements. The lookahead capability allows checking if a pair exists before processing. The last element is handled separately if the count is odd.
$ dart main.dart Pair: (10, 20) Pair: (20, 30) Pair: (30, 40) Single: 40
Custom Delimiter Joining
We can use HasNextIterator to implement custom string joining with lookahead.
import 'dart:collection'; String joinWithCommas(Iterable<String> items) { var buffer = StringBuffer(); var iterator = HasNextIterator(items.iterator); while (iterator.hasNext) { iterator.moveNext(); buffer.write(iterator.current); if (iterator.hasNext) { buffer.write(', '); } } return buffer.toString(); } void main() { var fruits = ['apple', 'banana', 'orange', 'mango']; print(joinWithCommas(fruits)); }
This implements a join function that only adds commas between elements, not after the last one. The hasNext check prevents the trailing comma.
$ dart main.dart apple, banana, orange, mango
Conditional Element Processing
HasNextIterator enables conditional processing based on upcoming elements.
import 'dart:collection'; void main() { var temperatures = [22.5, 23.1, 24.8, 25.3, 21.7].iterator; var iterator = HasNextIterator(temperatures); var warmDays = 0; while (iterator.hasNext) { iterator.moveNext(); var current = iterator.current; if (iterator.hasNext && iterator.peek > current) { print('$current°C (rising)'); warmDays++; } else { print('$current°C'); } } print('Warm days: $warmDays'); }
This example identifies temperature readings that are followed by higher values. The lookahead allows comparing the current element with the next one without consuming it.
$ dart main.dart 22.5°C (rising) 23.1°C (rising) 24.8°C (rising) 25.3°C 21.7°C Warm days: 3
Best Practices
- Check hasFirst: Always check hasNext before calling peek to avoid errors.
- Performance: HasNextIterator adds minimal overhead to the base iterator.
- Single Pass: Remember iterators are single-pass; don't reuse them.
- Combine with Other Iterators: Works well with other iterator transformers.
Source
Dart HasNextIterator Documentation
This tutorial covered Dart's HasNextIterator with practical examples demonstrating its lookahead capabilities and common usage patterns.
Author
List all Dart tutorials.