Class Iterables

java.lang.Object
apoc.util.collection.Iterables

public final class Iterables extends Object
Utility methods for processing iterables. Where possible, If the iterable implements Resource, it will be closed when the processing has been completed.
  • Constructor Details

    • Iterables

      public Iterables()
  • Method Details

    • addAll

      public static <T, C extends Collection<T>> C addAll(C collection, Iterable<? extends T> iterable)
      Collect all the elements available in iterable and add them to the provided collection.

      If the iterable implements Resource it will be closed in a finally block after all the items have been added.

      Type Parameters:
      T - the type of elements in iterable.
      C - the type of the collection to add the items to.
      Parameters:
      collection - the collection to add items to.
      iterable - the iterable from which items will be collected
      Returns:
      the collection that has been updated.
    • iterable

      @SafeVarargs public static <T, C extends T> Iterable<T> iterable(C... items)
    • asArray

      public static <T> T[] asArray(Class<T> componentType, Iterable<T> iterable)
    • asResourceIterable

      public static <T> org.neo4j.graphdb.ResourceIterable<T> asResourceIterable(Iterable<T> iterable)
    • firstOrNull

      public static <T> T firstOrNull(Iterable<T> iterable)
      Returns the given iterable's first element or null if no element found.

      If the iterable implements Resource, then it will be closed in a finally block after the first item has been retrieved, or failed to be retrieved.

      If the iterator created by the iterable 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 iterable.
      Parameters:
      iterable - the Iterable to get elements from.
      Returns:
      the first element in the iterable, or null if no element found.
    • first

      public static <T> T first(Iterable<T> iterable)
      Returns the given iterable's first element. If no element is found a NoSuchElementException is thrown.

      If the iterable implements Resource, then it will be closed in a finally block after the first item has been retrieved, or failed to be retrieved.

      Type Parameters:
      T - the type of elements in iterable.
      Parameters:
      iterable - the Iterable to get elements from.
      Returns:
      the first element in the iterable.
      Throws:
      NoSuchElementException - if no element found.
    • single

      public static <T> T single(Iterable<T> iterable)
      Returns the given iterable's single element. If there are no elements or more than one element in the iterable a NoSuchElementException will be thrown.

      If the iterable implements Resource, then it will be closed in a finally block after the single item has been retrieved, or failed to be retrieved.

      If the iterator created by the iterable 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 iterable.
      Parameters:
      iterable - the Iterable to get elements from.
      Returns:
      the single element in the iterable.
      Throws:
      NoSuchElementException - if there isn't exactly one element.
    • count

      public static <T> long count(Iterable<T> iterable)
      Counts the number of items in the iterable by looping through it.

      If the iterable implements Resource, then it will be closed in a finally block after all its items have been counted.

      If the iterator created by the iterable 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:
      iterable - the Iterable to count items in.
      Returns:
      the number of items found in iterable.
    • asList

      public static <T> List<T> asList(Iterable<T> iterable)
      Creates a list from an iterable.

      If the iterable implements Resource, then it will be closed in a finally block after all its items have been added.

      If the iterator created by the iterable implements Resource it will be closed in a finally block after all the items have been added.

      Type Parameters:
      T - The generic type of both the iterable and the list.
      Parameters:
      iterable - The iterable to create the list from.
      Returns:
      a list containing all items from the iterable.
    • asSet

      public static <T> Set<T> asSet(Iterable<T> iterable)
      Creates a Set from an Iterable.

      If the iterable implements Resource, then it will be closed in a finally block after all its items have been added.

      If the iterator created by the iterable implements Resource it will be closed in a finally block after all the items have been added.

      Type Parameters:
      T - The generic type of items.
      Parameters:
      iterable - The items to create the set from.
      Returns:
      a set containing all items from the Iterable.
    • stream

      public static <T> Stream<T> stream(Iterable<T> iterable)
      Create a stream from the given iterable.

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

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