Package apoc.util.collection
Class Iterators
java.lang.Object
apoc.util.collection.Iterators
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T>static <T> org.neo4j.graphdb.ResourceIterator<T>asResourceIterator(Iterator<T> iterator) static <T> Set<T>static <T> Set<T>asSet(T... items) Creates aSetfrom an array of items.anstatic <T> longCounts the number of items in theiteratorby looping through it.static <T> TReturns the given iterator's first element.static <T> TfirstOrNull(Iterator<T> iterator) Returns the given iterator's first element ornullif no element found.static <T> TReturns the given iterator's single element.static <T> TsingleOrNull(Iterator<T> iterator) Returns the given iterator's single element ornullif no element found.static <T> Stream<T>Create a stream from the given iterator.static voidtryCloseResource(Iterator<?> iterator) Close the providediteratorif it implementsResource.
-
Constructor Details
-
Iterators
public Iterators()
-
-
Method Details
-
firstOrNull
Returns the given iterator's first element ornullif no element found.- Type Parameters:
T- the type of elements initerator.- Parameters:
iterator- theIteratorto get elements from.- Returns:
- the first element in the
iterator, ornullif no element found.
-
first
Returns the given iterator's first element. If no element is found aNoSuchElementExceptionis thrown.- Type Parameters:
T- the type of elements initerator.- Parameters:
iterator- theIteratorto get elements from.- Returns:
- the first element in the
iterator. - Throws:
NoSuchElementException- if no element found.
-
singleOrNull
Returns the given iterator's single element ornullif no element found. If there is more than one element in the iterator aNoSuchElementExceptionwill be thrown. If theiteratorimplementsResourceit will beclosedin afinallyblock after the single item has been retrieved, or failed to be retrieved.- Type Parameters:
T- the type of elements initerator.- Parameters:
iterator- theIteratorto get elements from.- Returns:
- the single element in
iterator, ornullif no element found. - Throws:
NoSuchElementException- if more than one element was found.
-
single
Returns the given iterator's single element. If there are no elements or more than one element in the iterator aNoSuchElementExceptionwill be thrown. If theiteratorimplementsResourceit will beclosedin afinallyblock after the single item has been retrieved, or failed to be retrieved.- Type Parameters:
T- the type of elements initerator.- Parameters:
iterator- theIteratorto get elements from.- Returns:
- the single element in the
iterator. - Throws:
NoSuchElementException- if there isn't exactly one element.
-
count
Counts the number of items in theiteratorby looping through it. If theiteratorimplementsResourceit will beclosedin afinallyblock after the items have been counted.- Type Parameters:
T- the type of items in the iterator.- Parameters:
iterator- theIteratorto count items in.- Returns:
- the number of items found in
iterator.
-
asList
-
asSet
-
asSet
Creates aSetfrom an array of items.an- Type Parameters:
T- the type of the items- Parameters:
items- the items to add to the set.- Returns:
- the
Setcontaining the items.
-
asResourceIterator
-
stream
Create a stream from the given iterator.Note: returned stream needs to be closed via
BaseStream.close()if the given iterator implementsResource.- 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 isnull
-
tryCloseResource
Close the providediteratorif it implementsResource.- Parameters:
iterator- the iterator to check for closing
-