Class ReusableIterator<T>

  • Type Parameters:
    T - type of elements to iterate.
    All Implemented Interfaces:
    Iterable<T>

    public class ReusableIterator<T>
    extends Object
    implements Iterable<T>
    A utility class that allows to reuse an Iterator.

    Iterators returned by this class' iterator() method copy the elements of the given source iterator to a list as they are requested. Subsequent iterators then traverse the cached list (and continue to copy elements, if they haven't been requested before) allowing to arbitrarily re-iterate over the elements of the initial iterator.

    • Constructor Detail

      • ReusableIterator

        public ReusableIterator​(Iterator<T> iterator)
        Default constructor.
        Parameters:
        iterator - the source iterator
      • ReusableIterator

        public ReusableIterator​(Iterator<T> iterator,
                                List<T> cache)
        Constructor that allows to explicitly specify that cache to be used. Useful if e.g. the size is known beforehand.
        Parameters:
        iterator - the source iterator
        cache - the cache to use