Iterators as interface again


Object findIf(Iterator iter, Predicate test)
{
  while(iter.hasNext())
  {
    Object current = iter.next();
    if(test.test(current))
      return current;
  }
  return null;
}
It's easier to write an new Iterator than a Collection, or rather it's easier to satisfy the Iterator interace than the Collection interface. Consequently, by writing find to take an iterator, it's easier to apply to it to things with aren't a Collection, because all I have to do is write an Iterator.

Previously we used interators to hide the differences between different return types. Here we use an iterator to minimise coupling.

And so for no effort here, I've widen the possible uses, applicability of my find method. Now we can use our find method not only with built in iterators, but with our own iterators too. The reverse iterator we saw before, perhaps. We can search the tree, or a DOM tree, or whatever.