java.lang.Object
org.elasticsearch.common.collect.Iterators

public class Iterators extends Object
  • Constructor Details

    • Iterators

      public Iterators()
  • Method Details

    • single

      public static <T> Iterator<T> single(T element)
      Returns a single element iterator over the supplied value.
    • concat

      @SafeVarargs public static <T> Iterator<T> concat(Iterator<? extends T>... iterators)
    • forArray

      public static <T> Iterator<T> forArray(T[] array)
    • forRange

      public static <T> Iterator<T> forRange(int lowerBoundInclusive, int upperBoundExclusive, IntFunction<? extends T> fn)
    • map

      public static <T, U> Iterator<U> map(Iterator<? extends T> input, Function<T,? extends U> fn)
    • filter

      public static <T> Iterator<T> filter(Iterator<? extends T> input, Predicate<T> predicate)
      Parameters:
      input - An iterator over non-null values.
      predicate - The predicate with which to filter the input.
      Returns:
      an iterator which returns the values from input which match predicate.
    • limit

      public static <T> Iterator<T> limit(Iterator<? extends T> input, int n)
      Returns an iterator that yields at most the first n elements of the provided input iterator.
    • toList

      public static <T> List<T> toList(Iterator<T> iterator)
      Returns a list containing the elements of the provided iterator.
    • flatMap

      public static <T, U> Iterator<U> flatMap(Iterator<? extends T> input, Function<T,Iterator<? extends U>> fn)
    • failFast

      public static <T> Iterator<T> failFast(Iterator<T> input, BooleanSupplier isFailingSupplier)
      Returns an iterator over the same items as the provided input except that it stops yielding items (i.e. starts returning false from Iterator.hasNext() on failure.
    • enumerate

      public static <T, U> Iterator<U> enumerate(Iterator<? extends T> input, BiFunction<Integer,T,? extends U> fn)
      Enumerates the elements of an iterator together with their index, using a function to combine the pair together into the final items produced by the iterator.

      An example of its usage to enumerate a list of names together with their positional index in the list:

      
       Iterator<String> nameIterator = ...;
       Iterator<Tuple<Integer, String>> enumeratedNames = Iterators.enumerate(nameIterator, Tuple::new);
       enumeratedNames.forEachRemaining(tuple -> System.out.println("Index: " + t.v1() + ", Name: " + t.v2()));
       
      Type Parameters:
      T - The object type contained in the original iterator
      U - The object type that results from combining the original entry with its index in the iterator
      Parameters:
      input - The iterator to wrap
      fn - A function that takes the index for an entry and the entry itself, returning an item that combines them together
      Returns:
      An iterator that combines elements together with their indices in the underlying collection
    • fromSupplier

      public static <T> Iterator<T> fromSupplier(Supplier<? extends T> input)
      Adapts a Supplier object into an iterator. The resulting iterator will return values from the delegate Supplier until the delegate returns a null value. Once the delegate returns null, the iterator will claim to be empty.

      An example of its usage to iterate over a queue while draining it at the same time:

      
           LinkedList<String> names = ...;
           assert names.size() != 0;
      
           Iterator<String> nameIterator = Iterator.fromSupplier(names::pollFirst);
           nameIterator.forEachRemaining(System.out::println)
           assert names.size() == 0;
       
      Type Parameters:
      T - The object type returned from the supplier function
      Parameters:
      input - A Supplier that returns null when no more elements should be returned from the iterator
      Returns:
      An iterator that returns elements by calling the supplier until a null value is returned
    • equals

      public static <T> boolean equals(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2, BiPredicate<T,T> itemComparer)
    • hashCode

      public static <T> int hashCode(Iterator<? extends T> iterator, ToIntFunction<T> itemHashcode)