java.lang.Object
org.elasticsearch.common.collect.Iterators
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Iterator<T>
static <T,
U> Iterator<U> enumerate
(Iterator<? extends T> input, BiFunction<Integer, T, ? extends U> fn) Enumerates the elements of an iterator together with their index, using a function to combine the pair together into the final items produced by the iterator.static <T> boolean
equals
(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2, BiPredicate<T, T> itemComparer) static <T> Iterator<T>
failFast
(Iterator<T> input, BooleanSupplier isFailingSupplier) Returns an iterator over the same items as the providedinput
except that it stops yielding items (i.e.static <T> Iterator<T>
static <T,
U> Iterator<U> static <T> Iterator<T>
forArray
(T[] array) static <T> Iterator<T>
forRange
(int lowerBoundInclusive, int upperBoundExclusive, IntFunction<? extends T> fn) static <T> Iterator<T>
fromSupplier
(Supplier<? extends T> input) Adapts aSupplier
object into an iterator.static <T> int
hashCode
(Iterator<? extends T> iterator, ToIntFunction<T> itemHashcode) static <T> Iterator<T>
Returns an iterator that yields at most the firstn
elements of the providedinput
iterator.static <T,
U> Iterator<U> static <T> Iterator<T>
single
(T element) Returns a single element iterator over the supplied value.static <T> List<T>
Returns a list containing the elements of the providediterator
.
-
Constructor Details
-
Iterators
public Iterators()
-
-
Method Details
-
single
Returns a single element iterator over the supplied value. -
concat
-
forArray
-
forRange
public static <T> Iterator<T> forRange(int lowerBoundInclusive, int upperBoundExclusive, IntFunction<? extends T> fn) -
map
-
filter
- Parameters:
input
- An iterator over non-null values.predicate
- The predicate with which to filter the input.- Returns:
- an iterator which returns the values from
input
which matchpredicate
.
-
limit
Returns an iterator that yields at most the firstn
elements of the providedinput
iterator. -
toList
Returns a list containing the elements of the providediterator
. -
flatMap
-
failFast
Returns an iterator over the same items as the providedinput
except that it stops yielding items (i.e. starts returningfalse
fromIterator.hasNext()
on failure. -
enumerate
public static <T,U> Iterator<U> enumerate(Iterator<? extends T> input, BiFunction<Integer, T, ? extends U> fn) Enumerates the elements of an iterator together with their index, using a function to combine the pair together into the final items produced by the iterator.An example of its usage to enumerate a list of names together with their positional index in the list:
Iterator<String> nameIterator = ...; Iterator<Tuple<Integer, String>> enumeratedNames = Iterators.enumerate(nameIterator, Tuple::new); enumeratedNames.forEachRemaining(tuple -> System.out.println("Index: " + t.v1() + ", Name: " + t.v2()));
- Type Parameters:
T
- The object type contained in the original iteratorU
- The object type that results from combining the original entry with its index in the iterator- Parameters:
input
- The iterator to wrapfn
- A function that takes the index for an entry and the entry itself, returning an item that combines them together- Returns:
- An iterator that combines elements together with their indices in the underlying collection
-
fromSupplier
Adapts aSupplier
object into an iterator. The resulting iterator will return values from the delegate Supplier until the delegate returns anull
value. Once the delegate returnsnull
, the iterator will claim to be empty.An example of its usage to iterate over a queue while draining it at the same time:
LinkedList<String> names = ...; assert names.size() != 0; Iterator<String> nameIterator = Iterator.fromSupplier(names::pollFirst); nameIterator.forEachRemaining(System.out::println) assert names.size() == 0;
- Type Parameters:
T
- The object type returned from the supplier function- Parameters:
input
- ASupplier
that returns null when no more elements should be returned from the iterator- Returns:
- An iterator that returns elements by calling the supplier until a null value is returned
-
equals
public static <T> boolean equals(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2, BiPredicate<T, T> itemComparer) -
hashCode
-