Class IteratorToolkit


  • public class IteratorToolkit
    extends java.lang.Object
    Various methods that work with iterators.
    • Constructor Summary

      Constructors 
      Constructor Description
      IteratorToolkit()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Iterator<T> filter​(java.util.Iterator<T> itr, IPredicate<? super T> filter)
      Wrap an iterator in a new iterator that filters out values based on a predicate.
      static <T> java.util.Iterator<T> of​(T[] elements)
      Iterator that iterates over an array.
      (package private) static <T> java.util.Iterator<T> of​(T[] elements, int offset, int len)
      Iterator that iterates over a part of an array.
      static <T> java.util.Iterator<T> skipNulls​(java.util.Iterator<T> itr)
      Wrap an iterator in a new iterator that skips all null values.
      static <T> java.util.List<T> toList​(java.util.Iterator<T> itr, int sizeHint)
      Place all elements of an iterator in a list.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IteratorToolkit

        public IteratorToolkit()
    • Method Detail

      • toList

        public static <T> java.util.List<T> toList​(java.util.Iterator<T> itr,
                                                   int sizeHint)
        Place all elements of an iterator in a list.
        Type Parameters:
        T - input iterator type
        Parameters:
        itr - iterator
        sizeHint - a hint of how many elements there are
        Returns:
        a new list with all elements from the iterator
      • skipNulls

        public static <T> java.util.Iterator<T> skipNulls​(java.util.Iterator<T> itr)
        Wrap an iterator in a new iterator that skips all null values.
        Type Parameters:
        T - input iterator type
        Parameters:
        itr - input iterator that may produce null values
        Returns:
        a new iterator that will never produce null values
      • filter

        public static <T> java.util.Iterator<T> filter​(java.util.Iterator<T> itr,
                                                       IPredicate<? super T> filter)
        Wrap an iterator in a new iterator that filters out values based on a predicate.
        Type Parameters:
        T - input iterator type
        Parameters:
        itr - input iterator
        filter - filter predicate
        Returns:
        a new iterator that only contains values where the filter evaluates to true
      • of

        public static <T> java.util.Iterator<T> of​(T[] elements)
        Iterator that iterates over an array. Hopefully faster than Arrays.asList(...).iterator() since there are no concurrency checks.
        Type Parameters:
        T - input iterator type
        Parameters:
        elements - elements to iterate over
        Returns:
        an iterator
      • of

        static <T> java.util.Iterator<T> of​(T[] elements,
                                            int offset,
                                            int len)
        Iterator that iterates over a part of an array. Hopefully faster than Arrays.asList(...).iterator() since there are no concurrency checks.
        Type Parameters:
        T - input iterator type
        Parameters:
        elements - elements to iterate over
        offset - array index to start the iterator on
        len - number of elements to iterate over
        Returns:
        an iterator