Peekable Iterators in Java
In Java there is your standard Iterator that has been around “forever” (since Java 1.2) and has hasNext() and next() methods, to check if the iterator has more elements and to get the next one, respectively. This is often sufficient, and can work well (though imperatively). Java 5 added a new Iterable interface along with the “enhanced for” loop, from which you can get an Iterator and a couple other things (primarily a forEach() method). ...