Class Iterators

java.lang.Object
org.moeaframework.util.Iterators

public class Iterators extends Object
Collection of static methods for dealing with Iterables and Iterators.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Associates an index to a value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    count(Iterable<?> iterable)
    Counts the number of elements in the iterable.
    static int
    count(Iterator<?> iterator)
    Counts the number of elements in the iterator.
    enumerate(Iterable<T> iterable)
    Returns an iterable that tracks the index of each item.
    enumerate(Iterator<T> iterator)
    Returns an iterator that tracks the index of each item.
    enumerate(T[] array)
    Returns an iterable that tracks the index of each item.
    static <T> Iterable<T>
    join(Iterable<T>... iterables)
    Returns an iterable that joins the contents of multiple iterables.
    static <T> Iterator<T>
    join(Iterator<T>... iterators)
    Returns an iterator that joins the contents of multiple iterators.
    static <T> T
    last(Iterable<T> iterable)
    Returns the last element in the iterable.
    static <T> T
    last(Iterator<T> iterator)
    Returns the last element in the iterator.
    static <T, R> Iterable<R>
    map(Iterable<T> iterable, Function<T,R> function)
    Returns an iterable mapping the function to the items in the iterable.
    static <T, R> Iterator<R>
    map(Iterator<T> iterator, Function<T,R> function)
    Returns an iterator mapping the function to the items in the iterator.
    static <T, R> Iterable<R>
    map(T[] array, Function<T,R> function)
    Returns an iterable mapping the function to the items in the array.
    static <T> List<T>
    materialize(Iterable<T> iterable)
    Materializes the given iterable, returning a collection of all the items.
    static <T> List<T>
    materialize(Iterator<T> iterator)
    Materializes the given iterator, returning a collection of all the items.
    static <V extends Comparable<? super V>>
    V
    maximum(Iterable<V> iterable)
    Finds and returns the maximum element using natural ordering as defined by the Comparable.
    static <V extends Comparable<? super V>>
    V
    maximum(Iterator<V> iterator)
    Finds and returns the maximum element using natural ordering as defined by the Comparable.
    static <V extends Comparable<? super V>>
    V
    minimum(Iterable<V> iterable)
    Finds and returns the minimum element using natural ordering as defined by the Comparable.
    static <K extends Comparable<? super K>, V>
    org.apache.commons.lang3.tuple.Pair<K,V>
    minimum(Iterable<V> iterable, Function<? super V,? extends K> keyExtractor)
    Finds and returns the minimum element based on the given key with a natural ordering.
    static <V extends Comparable<? super V>>
    V
    minimum(Iterator<V> iterator)
    Finds and returns the minimum element using natural ordering as defined by the Comparable.
    static <K extends Comparable<? super K>, V>
    org.apache.commons.lang3.tuple.Pair<K,V>
    minimum(Iterator<V> iterator, Function<? super V,? extends K> keyExtractor)
    Finds and returns the minimum element based on the given key with a natural ordering.
    static <K, V> org.apache.commons.lang3.tuple.Pair<K,V>
    minimum(Iterator<V> iterator, Function<? super V,? extends K> keyExtractor, Comparator<? super K> keyComparator)
    Finds and returns the minimum element based on the given key with the ordering defined by a comparator function.
    static <T> Iterator<T>
    of(T... values)
    Returns an immutable iterator containing the given values.
    static <K, T> Iterable<org.apache.commons.lang3.tuple.Pair<K,T>>
    zip(Iterable<K> iterable1, Iterable<T> iterable2)
    Returns an iterable that returns pairs of items from two iterables.
    static <K, T> Iterator<org.apache.commons.lang3.tuple.Pair<K,T>>
    zip(Iterator<K> iterator1, Iterator<T> iterator2)
    Returns an iterator that returns pairs of items from two iterators.
    static <K, T> Iterable<org.apache.commons.lang3.tuple.Pair<K,T>>
    zip(K[] array1, T[] array2)
    Returns an iterable that returns pairs of items from two arrays.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      @SafeVarargs public static <T> Iterator<T> of(T... values)
      Returns an immutable iterator containing the given values.
      Type Parameters:
      T - the type of each value
      Parameters:
      values - the values
      Returns:
      the iterator over the values
    • join

      @SafeVarargs public static <T> Iterator<T> join(Iterator<T>... iterators)
      Returns an iterator that joins the contents of multiple iterators.
      Type Parameters:
      T - the type of each item
      Parameters:
      iterators - the iterators
      Returns:
      the joined iterator
    • join

      @SafeVarargs public static <T> Iterable<T> join(Iterable<T>... iterables)
      Returns an iterable that joins the contents of multiple iterables.
      Type Parameters:
      T - the type of each item
      Parameters:
      iterables - the iterables
      Returns:
      the joined iterable
    • enumerate

      public static <T> Iterator<Iterators.IndexedValue<T>> enumerate(Iterator<T> iterator)
      Returns an iterator that tracks the index of each item.
      Type Parameters:
      T - the type of each item
      Parameters:
      iterator - the iterator
      Returns:
      the indexed value
    • enumerate

      public static <T> Iterable<Iterators.IndexedValue<T>> enumerate(Iterable<T> iterable)
      Returns an iterable that tracks the index of each item.
      Type Parameters:
      T - the type of each item
      Parameters:
      iterable - the iterable
      Returns:
      the indexed iterable
    • enumerate

      public static <T> Iterable<Iterators.IndexedValue<T>> enumerate(T[] array)
      Returns an iterable that tracks the index of each item.
      Type Parameters:
      T - the type of each item
      Parameters:
      array - the array of items
      Returns:
      the indexed iterable
    • zip

      public static <K, T> Iterator<org.apache.commons.lang3.tuple.Pair<K,T>> zip(Iterator<K> iterator1, Iterator<T> iterator2)
      Returns an iterator that returns pairs of items from two iterators.
      Type Parameters:
      K - the type of the first iterator
      T - the type of the second iterator
      Parameters:
      iterator1 - the first iterator
      iterator2 - the second iterator
      Returns:
      an iterator over pairs
    • zip

      public static <K, T> Iterable<org.apache.commons.lang3.tuple.Pair<K,T>> zip(Iterable<K> iterable1, Iterable<T> iterable2)
      Returns an iterable that returns pairs of items from two iterables.
      Type Parameters:
      K - the type of the first iterable
      T - the type of the second iterable
      Parameters:
      iterable1 - the first iterable
      iterable2 - the second iterable
      Returns:
      an iterable over pairs
    • zip

      public static <K, T> Iterable<org.apache.commons.lang3.tuple.Pair<K,T>> zip(K[] array1, T[] array2)
      Returns an iterable that returns pairs of items from two arrays.
      Type Parameters:
      K - the type of the first array
      T - the type of the second array
      Parameters:
      array1 - the first array
      array2 - the second array
      Returns:
      an iterable over pairs
    • map

      public static <T, R> Iterator<R> map(Iterator<T> iterator, Function<T,R> function)
      Returns an iterator mapping the function to the items in the iterator.
      Type Parameters:
      T - the source type
      R - the result type
      Parameters:
      iterator - the iterator
      function - the mapping function
      Returns:
      the result iterator
    • map

      public static <T, R> Iterable<R> map(Iterable<T> iterable, Function<T,R> function)
      Returns an iterable mapping the function to the items in the iterable.
      Type Parameters:
      T - the source type
      R - the result type
      Parameters:
      iterable - the iterable
      function - the mapping function
      Returns:
      the result iterable
    • map

      public static <T, R> Iterable<R> map(T[] array, Function<T,R> function)
      Returns an iterable mapping the function to the items in the array.
      Type Parameters:
      T - the source type
      R - the result type
      Parameters:
      array - the array
      function - the mapping function
      Returns:
      the result iterable
    • materialize

      public static <T> List<T> materialize(Iterator<T> iterator)
      Materializes the given iterator, returning a collection of all the items.
      Type Parameters:
      T - the type of the iterator
      Parameters:
      iterator - the iterator
      Returns:
      the collection of items
    • materialize

      public static <T> List<T> materialize(Iterable<T> iterable)
      Materializes the given iterable, returning a collection of all the items.
      Type Parameters:
      T - the type of the iterable
      Parameters:
      iterable - the iterable
      Returns:
      the collection of items
    • last

      public static <T> T last(Iterator<T> iterator)
      Returns the last element in the iterator.
      Type Parameters:
      T - the type of the iterator
      Parameters:
      iterator - the iterator
      Returns:
      the last element, or null if the iterator was empty
    • last

      public static <T> T last(Iterable<T> iterable)
      Returns the last element in the iterable.
      Type Parameters:
      T - the type of the iterable
      Parameters:
      iterable - the iterable
      Returns:
      the last element, or null if the iterable was empty
    • count

      public static int count(Iterator<?> iterator)
      Counts the number of elements in the iterator.
      Parameters:
      iterator - the iterator
      Returns:
      the number of elements
    • count

      public static int count(Iterable<?> iterable)
      Counts the number of elements in the iterable.
      Parameters:
      iterable - the iterable
      Returns:
      the number of elements
    • minimum

      public static <V extends Comparable<? super V>> V minimum(Iterator<V> iterator)
      Finds and returns the minimum element using natural ordering as defined by the Comparable.
      Type Parameters:
      V - the comparable type
      Parameters:
      iterator - the iterator
      Returns:
      the minimum element
      Throws:
      NoSuchElementException - if the iterator is empty
    • maximum

      public static <V extends Comparable<? super V>> V maximum(Iterator<V> iterator)
      Finds and returns the maximum element using natural ordering as defined by the Comparable.
      Type Parameters:
      V - the comparable type
      Parameters:
      iterator - the iterator
      Returns:
      the maximum element
      Throws:
      NoSuchElementException - if the iterator is empty
    • minimum

      public static <V extends Comparable<? super V>> V minimum(Iterable<V> iterable)
      Finds and returns the minimum element using natural ordering as defined by the Comparable.
      Type Parameters:
      V - the comparable type
      Parameters:
      iterable - the iterable
      Returns:
      the minimum element
      Throws:
      NoSuchElementException - if the iterator is empty
    • maximum

      public static <V extends Comparable<? super V>> V maximum(Iterable<V> iterable)
      Finds and returns the maximum element using natural ordering as defined by the Comparable.
      Type Parameters:
      V - the comparable type
      Parameters:
      iterable - the iterable
      Returns:
      the maximum element
      Throws:
      NoSuchElementException - if the iterator is empty
    • minimum

      public static <K extends Comparable<? super K>, V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterator<V> iterator, Function<? super V,? extends K> keyExtractor)
      Finds and returns the minimum element based on the given key with a natural ordering.
      Type Parameters:
      K - the type of the key used for comparison
      V - the type of each element
      Parameters:
      iterator - the iterator
      keyExtractor - function returning the key used for comparisons
      Returns:
      the minimum key and element
      Throws:
      NoSuchElementException - if the iterator is empty
    • minimum

      public static <K extends Comparable<? super K>, V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterable<V> iterable, Function<? super V,? extends K> keyExtractor)
      Finds and returns the minimum element based on the given key with a natural ordering.
      Type Parameters:
      K - the type of the key used for comparison
      V - the type of each element
      Parameters:
      iterable - the iterable
      keyExtractor - function returning the key used for comparisons
      Returns:
      the minimum key and element
      Throws:
      NoSuchElementException - if the iterable is empty
    • minimum

      public static <K, V> org.apache.commons.lang3.tuple.Pair<K,V> minimum(Iterator<V> iterator, Function<? super V,? extends K> keyExtractor, Comparator<? super K> keyComparator)
      Finds and returns the minimum element based on the given key with the ordering defined by a comparator function.
      Type Parameters:
      K - the type of the key used for comparison
      V - the type of each element
      Parameters:
      iterator - the iterator
      keyExtractor - function returning the key used for comparisons
      keyComparator - function for ordering the keys
      Returns:
      the minimum key and element
      Throws:
      NoSuchElementException - if the iterator is empty