Class Iterators


  • public final class Iterators
    extends Object
    Methods for manipulating iterators
    Author:
    David B. Bracewell
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Iterator<?> asIterator​(Object object)
      As iterator iterator.
      static <T> Iterator<T> concat​(Iterable<? extends T>... iterables)
      Concatenates iterables together
      static <T> Iterator<T> concat​(Iterator<? extends T>... iterators)
      Concatenates iterators together
      static <E> Iterator<E> filter​(Iterator<? extends E> iterator, SerializablePredicate<? super E> filter)
      Filters elements from the given iterator when the given filter predicate evaluates to false
      static <T> Optional<T> first​(Iterator<? extends T> iterator)
      Gets the first element of the iterator that would be returned by calling next if it exists.
      static <T> T first​(Iterator<? extends T> iterator, T defaultValue)
      Gets the first element of the iterator that would be returned by calling next if it exists or the default value.
      static <T> Iterator<T> flatten​(Iterator<? extends Iterator<? extends T>> iterator)
      Flattens an iterator of iterators.
      static <T> Optional<T> get​(Iterator<? extends T> iterator, int n)
      Gets the element of the iterator after iterator index times if it exists
      static <T> T get​(Iterator<? extends T> iterator, int n, T defaultValue)
      Gets the element of the iterator after iterator index times if it exists and if it does not exist, returns the default value
      static <T> Optional<T> last​(Iterator<? extends T> iterator)
      Gets the last element of the iterator that would be returned by calling next until hasNext returns false if it exists.
      static <T> T last​(Iterator<? extends T> iterator, T defaultValue)
      Gets the last element of the iterator that would be returned by calling next until hasNext returns false if it exists or the default value.
      static <T> Optional<T> next​(Iterator<? extends T> iterator)
      Gets the next element of the iterator that would be returned by calling next if it exists.
      static <T> T next​(Iterator<? extends T> iterator, T defaultValue)
      Gets the next element of the iterator that would be returned by calling next if it exists or the default value.
      static <T> Iterator<List<T>> partition​(Iterator<? extends T> iterator, int partitionSize, boolean pad)
      Partitions the elements in the iterator into a number of lists equal to partition size except for the last partition, which may have less elements if pad is false but will be filled with nulls if true.
      static <T> Iterator<List<T>> partition​(Iterator<T> iterator, int partitionSize)
      Partitions the elements in the iterator into a number of lists equal to partition size except for the last partition, which may have less elements.
      static <T> Iterator<T> singletonIterator​(T object)  
      static int size​(Iterator<?> iterator)
      Gets the number of items in the iterator
      static <I,​O>
      Iterator<O>
      transform​(Iterator<? extends I> iterator, SerializableFunction<? super I,​? extends O> function)
      Creates an iterator that transforms the elements of the iterator
      static <T> Iterator<T> unmodifiableIterator​(Iterator<? extends T> iterator)
      Wraps an iterator allowing items to not be removed
      static <T,​U>
      Iterator<Map.Entry<T,​U>>
      zip​(Iterator<? extends T> iterator1, Iterator<? extends U> iterator2)
      Zips (combines) two iterators together.
      static <T> Iterator<Map.Entry<T,​Integer>> zipWithIndex​(Iterator<? extends T> iterator)
      Creates pairs of entries from the given iterator and its index in the iterator (0 based)
    • Method Detail

      • asIterator

        public static Iterator<?> asIterator​(Object object)
        As iterator iterator.
        Parameters:
        object - the object
        Returns:
        the iterator
      • concat

        @SafeVarargs
        public static <T> Iterator<T> concat​(Iterator<? extends T>... iterators)
        Concatenates iterators together
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterators - the iterators to concatenate
        Returns:
        the concatenated iterator
      • concat

        @SafeVarargs
        public static <T> Iterator<T> concat​(Iterable<? extends T>... iterables)
        Concatenates iterables together
        Type Parameters:
        T - the iterables element type parameter
        Parameters:
        iterables - the iterables to concatenate
        Returns:
        the concatenated iterator
      • filter

        public static <E> Iterator<E> filter​(Iterator<? extends E> iterator,
                                             SerializablePredicate<? super E> filter)
        Filters elements from the given iterator when the given filter predicate evaluates to false
        Type Parameters:
        E - the iterator element parameter
        Parameters:
        iterator - the iterator to filter
        filter - the filter to apply items evaluating to false will be removed from the iterator
        Returns:
        the filtered iterator
      • first

        public static <T> T first​(Iterator<? extends T> iterator,
                                  T defaultValue)
        Gets the first element of the iterator that would be returned by calling next if it exists or the default value.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        defaultValue - the default value
        Returns:
        the first element in the iterator or the default value
      • first

        public static <T> Optional<T> first​(Iterator<? extends T> iterator)
        Gets the first element of the iterator that would be returned by calling next if it exists.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        Returns:
        the first element in the iterator
      • flatten

        public static <T> Iterator<T> flatten​(Iterator<? extends Iterator<? extends T>> iterator)
        Flattens an iterator of iterators.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        Returns:
        the flattened iterator
      • get

        public static <T> Optional<T> get​(Iterator<? extends T> iterator,
                                          int n)
        Gets the element of the iterator after iterator index times if it exists
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        n - how many times to iterate
        Returns:
        Optional of the element of the iterator after iterating n times if it exists
      • get

        public static <T> T get​(Iterator<? extends T> iterator,
                                int n,
                                T defaultValue)
        Gets the element of the iterator after iterator index times if it exists and if it does not exist, returns the default value
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        n - how many times to iterate
        defaultValue - the default value to return if nothing is at the given index
        Returns:
        the element of the iterator after iterating n times or default value if cannot iterator n times
      • last

        public static <T> T last​(Iterator<? extends T> iterator,
                                 T defaultValue)
        Gets the last element of the iterator that would be returned by calling next until hasNext returns false if it exists or the default value.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        defaultValue - the default value
        Returns:
        the last element in the iterator or the default value
      • last

        public static <T> Optional<T> last​(Iterator<? extends T> iterator)
        Gets the last element of the iterator that would be returned by calling next until hasNext returns false if it exists.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        Returns:
        the last element in the iterator
      • next

        public static <T> Optional<T> next​(Iterator<? extends T> iterator)
        Gets the next element of the iterator that would be returned by calling next if it exists.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        Returns:
        the next element in the iterator
      • next

        public static <T> T next​(Iterator<? extends T> iterator,
                                 T defaultValue)
        Gets the next element of the iterator that would be returned by calling next if it exists or the default value.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator
        defaultValue - the default value
        Returns:
        the next element in the iterator or the default value
      • partition

        public static <T> Iterator<List<T>> partition​(Iterator<T> iterator,
                                                      int partitionSize)
        Partitions the elements in the iterator into a number of lists equal to partition size except for the last partition, which may have less elements.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator to partition
        partitionSize - the partition size
        Returns:
        the partitioned iterator
      • partition

        public static <T> Iterator<List<T>> partition​(Iterator<? extends T> iterator,
                                                      int partitionSize,
                                                      boolean pad)
        Partitions the elements in the iterator into a number of lists equal to partition size except for the last partition, which may have less elements if pad is false but will be filled with nulls if true.
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator to partition
        partitionSize - the partition size
        pad - true add nulls to ensure list size is equal to partition size
        Returns:
        the partitioned iterator
      • singletonIterator

        public static <T> Iterator<T> singletonIterator​(T object)
      • size

        public static int size​(Iterator<?> iterator)
        Gets the number of items in the iterator
        Parameters:
        iterator - the iterator
        Returns:
        the size or number of items in the iterator
      • transform

        public static <I,​O> Iterator<O> transform​(Iterator<? extends I> iterator,
                                                        SerializableFunction<? super I,​? extends O> function)
        Creates an iterator that transforms the elements of the iterator
        Type Parameters:
        I - the type parameter of the item in given iterator
        O - the type parameter of the output of the transform function
        Parameters:
        iterator - the iterator to transform
        function - the transform function
        Returns:
        the transformed iterator
      • unmodifiableIterator

        public static <T> Iterator<T> unmodifiableIterator​(Iterator<? extends T> iterator)
        Wraps an iterator allowing items to not be removed
        Type Parameters:
        T - the iterator element type parameter
        Parameters:
        iterator - the iterator to make unmodifiable
        Returns:
        the unmodifiable iterator
      • zip

        public static <T,​U> Iterator<Map.Entry<T,​U>> zip​(Iterator<? extends T> iterator1,
                                                                     Iterator<? extends U> iterator2)

        Zips (combines) two iterators together. For example, if iterator 1 contained [1,2,3] and iterator 2 contained [4,5,6] the result would be [(1,4), (2,5), (3,6)]. Note that the length of the resulting stream will be the minimum of the two iterators.

        Type Parameters:
        T - the component type of the first iterator
        U - the component type of the second iterator
        Parameters:
        iterator1 - the iterator making up the key in the resulting entries
        iterator2 - the iterator making up the value in the resulting entries
        Returns:
        A stream of entries whose keys are taken from iterator1 and values are taken from iterator2
      • zipWithIndex

        public static <T> Iterator<Map.Entry<T,​Integer>> zipWithIndex​(Iterator<? extends T> iterator)
        Creates pairs of entries from the given iterator and its index in the iterator (0 based)
        Type Parameters:
        T - the iterator type parameter
        Parameters:
        iterator - the iterator
        Returns:
        the iterator with index values