Class Iterators

java.lang.Object
apoc.util.collection.Iterators

public final class Iterators extends Object
Contains common functionality regarding Iterators and Iterables.
  • Constructor Details

    • Iterators

      public Iterators()
  • Method Details

    • firstOrNull

      public static <T> T firstOrNull(Iterator<T> iterator)
      Returns the given iterator's first element or null if no element found.
      Type Parameters:
      T - the type of elements in iterator.
      Parameters:
      iterator - the Iterator to get elements from.
      Returns:
      the first element in the iterator, or null if no element found.
    • first

      public static <T> T first(Iterator<T> iterator)
      Returns the given iterator's first element. If no element is found a NoSuchElementException is thrown.
      Type Parameters:
      T - the type of elements in iterator.
      Parameters:
      iterator - the Iterator to get elements from.
      Returns:
      the first element in the iterator.
      Throws:
      NoSuchElementException - if no element found.
    • singleOrNull

      public static <T> T singleOrNull(Iterator<T> iterator)
      Returns the given iterator's single element or null if no element found. If there is more than one element in the iterator a NoSuchElementException will be thrown. If the iterator implements Resource it will be closed in a finally block after the single item has been retrieved, or failed to be retrieved.
      Type Parameters:
      T - the type of elements in iterator.
      Parameters:
      iterator - the Iterator to get elements from.
      Returns:
      the single element in iterator, or null if no element found.
      Throws:
      NoSuchElementException - if more than one element was found.
    • single

      public static <T> T single(Iterator<T> iterator)
      Returns the given iterator's single element. If there are no elements or more than one element in the iterator a NoSuchElementException will be thrown. If the iterator implements Resource it will be closed in a finally block after the single item has been retrieved, or failed to be retrieved.
      Type Parameters:
      T - the type of elements in iterator.
      Parameters:
      iterator - the Iterator to get elements from.
      Returns:
      the single element in the iterator.
      Throws:
      NoSuchElementException - if there isn't exactly one element.
    • count

      public static <T> long count(Iterator<T> iterator)
      Counts the number of items in the iterator by looping through it. If the iterator implements Resource it will be closed in a finally block after the items have been counted.
      Type Parameters:
      T - the type of items in the iterator.
      Parameters:
      iterator - the Iterator to count items in.
      Returns:
      the number of items found in iterator.
    • asList

      public static <T> List<T> asList(Iterator<T> iterator)
    • asSet

      public static <T> Set<T> asSet(Iterator<T> iterator)
    • asSet

      @SafeVarargs public static <T> Set<T> asSet(T... items)
      Creates a Set from an array of items.an
      Type Parameters:
      T - the type of the items
      Parameters:
      items - the items to add to the set.
      Returns:
      the Set containing the items.
    • asResourceIterator

      public static <T> org.neo4j.graphdb.ResourceIterator<T> asResourceIterator(Iterator<T> iterator)
    • stream

      public static <T> Stream<T> stream(Iterator<T> iterator)
      Create a stream from the given iterator.

      Note: returned stream needs to be closed via BaseStream.close() if the given iterator implements Resource.

      Type Parameters:
      T - the type of elements in the given iterator
      Parameters:
      iterator - the iterator to convert to stream
      Returns:
      stream over the iterator elements
      Throws:
      NullPointerException - when the given stream is null
    • tryCloseResource

      public static void tryCloseResource(Iterator<?> iterator)
      Close the provided iterator if it implements Resource.
      Parameters:
      iterator - the iterator to check for closing