Class IteratorToolkit

java.lang.Object
org.openjdk.jmc.common.collection.IteratorToolkit

public class IteratorToolkit extends Object
Various methods that work with iterators.
  • Constructor Details

    • IteratorToolkit

      public IteratorToolkit()
  • Method Details

    • toList

      public static <T> List<T> toList(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> Iterator<T> skipNulls(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> Iterator<T> filter(Iterator<T> itr, Predicate<? 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> 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