Where are we?
Iterators as extension
Iterators for alternative traversal
Iterators as interface
Iterators as common currency
I've spent a while now talking about what iteration is, and about writing iterators for different things, or providing alternative iterators for things.
Extension - eg ReverseIterator effectively provides new functionality to Lists.
Alternative traversal - Pre/post/in-order for trees. Reverse for Lists
Interface - elimination of null, in a web framework that can be a lot of nulls
Common currency - when we loop, we're generally more interesting in the contents than the container, the loop body is the interesting part. Eg args to main is a String[], but if it were a list we wouldn't do anything different.
Ok, that's fine but isn't it all a bit computer sciencey? We have things to do - code to write, apps to ship. Why should I bother writing these iterators, when I'm doing fine as it is, thanks.
Let's look at this from the other direction. So far, when I've presented code it's featured the iterator but not what we're doing with the elements we get back. Rather than tweaking our notion of what an iterator does, let's think about libraries. A library is, by definition, code you want to use (or hope to) use again.
Noting only that java.util is a really, really bad package name, the Java Collections gives us a number of different collection types, described as iterfaces. There are unordered types - sets, ordered types - lists, associated types - maps, and there are various unsynchronised and synchronised versions.
A collection is useful thing, which is clearly why we use them all the time. We search them, sort them, traverse them in different ways - forwards and backwards, compare different collections, find the elements in one that are in another, and so on. Those are the kind of operations you might expect a collections library to provide for us.
Anybody like to hazard a guess as to how many collection operations/functions/algorithms the Java Collections library provides.