Class Iterators

java.lang.Object
fr.lirmm.graphik.util.stream.Iterators

public final class Iterators extends Object
Helper methods for creating and collecting closeable iterators.
Author:
Clément Sipieter (INRIA) <clement@6pi.fr>
  • Method Details

    • emptyIterator

      public static <T> CloseableIteratorWithoutException<T> emptyIterator()
      Construct and return an empty iterator. The hasNext method will always return false and the next method will always return null.
      Type Parameters:
      T - the element type
      Returns:
      An instance of a CloseableIteratorWithoutException
    • singletonIterator

      public static <E> CloseableIteratorWithoutException<E> singletonIterator(E e)
      Creates an iterator yielding a single element.
      Type Parameters:
      E - the element type
      Parameters:
      e - the single element to iterate over
      Returns:
      an iterator containing only e
    • count

      public static int count(CloseableIterator<?> it) throws IteratorException
      Counts the number of remaining elements in an iterator.
      Parameters:
      it - the iterator to consume
      Returns:
      the number of remaining elements
      Throws:
      IteratorException - if the iterator cannot be consumed
    • count

      public static int count(CloseableIteratorWithoutException<?> it)
      Counts the number of remaining elements in an iterator without checked exceptions.
      Parameters:
      it - the iterator to consume
      Returns:
      the number of remaining elements
    • uniqLocaly

      public static <T> CloseableIterator<T> uniqLocaly(CloseableIterator<T> it)
      Remove adjacent equals elements.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to filter
      Returns:
      a CloseableIterator.
    • sort

      public static <T extends Comparable<T>> CloseableIterator<T> sort(CloseableIterator<T> it) throws IteratorException
      Return an iterator over sorted elements from the specified iterator.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to sort
      Returns:
      a sorted CloseableIterator over elements from the specified Iterator.
      Throws:
      IteratorException - if the iterator cannot be consumed
    • sort

      public static <T extends Comparable<T>> Iterator<T> sort(Iterator<T> it)
      Return an iterator over sorted elements from the specified iterator.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to sort
      Returns:
      a sorted Iterator over elements from the specified Iterator.
    • uniq

      public static <T extends Comparable<T>> CloseableIterator<T> uniq(CloseableIterator<T> it) throws IteratorException
      Remove all equals elements.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to deduplicate
      Returns:
      a CloseableIterator over elements from the specified it without duplicate.
      Throws:
      IteratorException - if the iterator cannot be consumed
    • uniq

      public static <T extends Comparable<T>> Iterator<T> uniq(Iterator<T> it)
      Remove all equals elements.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to deduplicate
      Returns:
      an Iterator over elements from the specified it without duplicate.
    • toList

      public static <T> List<T> toList(CloseableIterator<T> it) throws IteratorException
      Collects all remaining elements from an iterator into a list.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to consume
      Returns:
      a List containing all elements accessible by the specified iterator.
      Throws:
      IteratorException - if the iterator cannot be consumed
    • toList

      public static <T> List<T> toList(CloseableIteratorWithoutException<T> it)
      Collects all remaining elements from an iterator into a list.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to consume
      Returns:
      a List containing all elements accessible by the specified iterator.
    • toSet

      public static <T> Set<T> toSet(CloseableIterator<T> it) throws IteratorException
      Collects all remaining elements from an iterator into a set.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to consume
      Returns:
      a Set containing all elements accessible by the specified iterator.
      Throws:
      IteratorException - if the iterator cannot be consumed
    • toSet

      public static <T> Set<T> toSet(CloseableIteratorWithoutException<T> it)
      Collects all remaining elements from an iterator into a set.
      Type Parameters:
      T - the element type
      Parameters:
      it - the iterator to consume
      Returns:
      a Set containing all elements accessible by the specified iterator.