4 ms·
How is it not clear? "boolean hasNext() Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rathe
by LgWoodenBadger 8y ago
How is it not clear?
"boolean hasNext()
Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
Returns:
true if the iteration has more elements"
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html#hasNext-- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator...
Also, the for-each construct takes an Iterable, not an Iterator, though Iterable produces Iterators.
- wffurr 8y agoIt depends on whether the underlying stream supports "peek". If not, in order to determine whether there is a next element, the iterator must consume the next element and hold it in local memory until a call to next. That can matter in certain cases of large objects or slow or side-effecting stream access