java boilerplate

Thursday, December 01, 2005

A java.util.Iterators utility class

Analogous to java.util.Arrays and java.util.Collections, introduce a java.util.Iterators class which contains some useful utility methods for interfaces. Some useful methods:

  • A method that turns an interator into an iterable

  • A 'filter' iterator that takes as arguments another iterator and a mapping function, and returns a filter iterator. For example:
         Iterator i = //Something
    Iterator i2 = Iterators.unmodifiableIteratorAdapter(i,
    Adapter<String, Integer>(String e) {return Integer.valueOf(e);});


    The Adapter interface is new and looks like this:
         public @Function interface Adapter<I, O> {
    O filter(I input);
    }

  • A method that turns an array into an iterator

  • A method that returns a properly generics-typed empty iterator


The code examples here use the @Function interface idea described elsewhere in this blog.

We have an example Iterators implementation (does not use the @Function suggestion - it can be used in java 1.5). Fully documented:

Iterators.java.

0 Comments:

Post a Comment

<< Home