public interface CloseableIterator<T> extends Iterator<T>, Closeable
void method(CloseableIterable<T> ballotsIterable) {
try (CloseableIterator<T> ballotsIter = ballotsIterable.iterator()) {
while (ballotsIter.hasNext()) {
T ballot = ballotsIter.next();
// do stuff
}
}
}
or
void method(CloseableIterator<T> ballotsIter) {
try (Stream<T> ballotsStream = ballotsIter.stream()) {
ballotsStream.filter(b -> b.state == State.CAST)
.forEach(ballot -> {
// do stuff
});
}
}
The convention is that if CloseableIterator is passed to a method, it is not already in a try-finally block.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
IOException is converted to RuntimeException.
|
default Spliterator<T> |
spliterator()
Could be parallel.
|
default Stream<T> |
stream()
Convert to a stream, can be declared as a resource in a try-with-resources statement.
|
forEachRemaining, hasNext, next, removevoid close()
close in interface AutoCloseableclose in interface Closeabledefault Spliterator<T> spliterator()