Class StridingIterator<T>

java.lang.Object
com.github.tommyettinger.ds.support.util.StridingIterator<T>
Type Parameters:
T - the type of items this can return, and the type the wrapped Iterator returns
All Implemented Interfaces:
Iterator<T>

public class StridingIterator<T> extends Object implements Iterator<T>
Wraps an Iterator so that it starts at an offset, skipping that many items, then returning items that match a given stride, such as every other item, or every tenth item. If the offset is 0, this will try to return the first item and then any items matching the stride, so if the stride is 2, it will return the first item, skip one item, return the item after that, skip, return, skip, return, etc. This has undefined behavior if any items the Iterator could return are modified during iteration.
You can change the Iterator, offset, and stride at once using set(Iterator, int, int), and can also just change the Iterator with set(Iterator).
  • Field Details

    • iterator

      public Iterator<T> iterator
    • offset

      protected int offset
    • stride

      protected int stride
    • index

      protected int index
    • end

      protected boolean end
    • available

      protected boolean available
    • next

      protected T next
  • Constructor Details

    • StridingIterator

      public StridingIterator()
    • StridingIterator

      public StridingIterator(Iterator<T> iterator, int offset, int stride)
  • Method Details

    • set

      public void set(Iterator<T> iterator, int offset, int stride)
    • set

      public void set(Iterator<T> iterator)
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
    • remove

      public void remove()
      NOTE: this does not change the stride or offset, so the same sequence of values will be returned regardless of if some elements are removed with this method.
      Specified by:
      remove in interface Iterator<T>