Package com.landawn.abacus.util
Class Iterators
java.lang.Object
com.landawn.abacus.util.Iterators
This is a utility class mostly for Iterator
.
The methods in this class should only read the input Collection/Array/Iterator
parameters, not modify them.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
Note: copied from Google Guava under Apache license v2
Callsnext()
oniterator
, eithernumberToAdvance
times or untilhasNext()
returnsfalse
, whichever comes first.static BooleanIterator
concat
(boolean[]... a) static ByteIterator
concat
(byte[]... a) static DoubleIterator
concat
(double[]... a) static FloatIterator
concat
(float[]... a) static IntIterator
concat
(int[]... a) static LongIterator
concat
(long[]... a) static ShortIterator
concat
(short[]... a) static <A,
B> BiIterator<A, B> concat
(BiIterator<A, B>... a) static BooleanIterator
concat
(BooleanIterator... a) static ByteIterator
concat
(ByteIterator... a) static CharIterator
concat
(CharIterator... a) static DoubleIterator
concat
(DoubleIterator... a) static FloatIterator
concat
(FloatIterator... a) static IntIterator
concat
(IntIterator... a) static LongIterator
concat
(LongIterator... a) static ShortIterator
concat
(ShortIterator... a) static <A,
B, C> TriIterator<A, B, C> concat
(TriIterator<A, B, C>... a) static <T> ObjIterator<T>
static <T> ObjIterator<T>
concat
(Collection<? extends Iterator<? extends T>> c) static <T> ObjIterator<T>
static <K,
V> ObjIterator<Map.Entry<K, V>> static <T> ObjIterator<T>
concat
(T[]... a) static <T> ObjIterator<T>
concatIterables
(Collection<? extends Iterable<? extends T>> c) static long
static <T,
E extends Exception>
longcount
(Iterator<? extends T> iter, Throwables.Predicate<? super T, E> filter) static <T> ObjIterator<T>
static <T> ObjIterator<T>
distinctBy
(Iterator<? extends T> iter, Function<? super T, ?> keyMapper) static <T> ObjIterator<T>
static <T> ObjIterator<T>
static <T,
U> ObjIterator<U> flatMap
(Iterator<? extends T> iter, Function<? super T, ? extends Collection<? extends U>> mapper) static <T,
U> ObjIterator<U> static <T,
U> ObjIterator<T> generate
(U init, BiPredicate<? super U, T> hasNext, BiFunction<? super U, T, T> supplier) static <T,
U> ObjIterator<T> static <T> u.Nullable<T>
static <T> ObjIterator<T>
Returns a newIterator
.static <T,
U> ObjIterator<U> static <T> ObjIterator<T>
merge
(Iterable<? extends T> a, Iterable<? extends T> b, BiFunction<? super T, ? super T, MergeResult> nextSelector) static <T> ObjIterator<T>
merge
(Collection<? extends Iterator<? extends T>> c, BiFunction<? super T, ? super T, MergeResult> nextSelector) static <T> ObjIterator<T>
merge
(Iterator<? extends T> a, Iterator<? extends T> b, BiFunction<? super T, ? super T, MergeResult> nextSelector) static <T> ObjIterator<T>
mergeCollections
(Collection<? extends Collection<? extends T>> collections, BiFunction<? super T, ? super T, MergeResult> nextSelector) Deprecated.static <T> ObjIterator<T>
mergeIterables
(Collection<? extends Iterable<? extends T>> iterables, BiFunction<? super T, ? super T, MergeResult> nextSelector) static <T extends Comparable>
ObjIterator<T>mergeSorted
(Iterable<? extends T> a, Iterable<? extends T> b) static <T> ObjIterator<T>
mergeSorted
(Iterable<? extends T> a, Iterable<? extends T> b, Comparator<? super T> cmp) static <T extends Comparable>
ObjIterator<T>mergeSorted
(Iterator<? extends T> a, Iterator<? extends T> b) static <T> ObjIterator<T>
mergeSorted
(Iterator<? extends T> a, Iterator<? extends T> b, Comparator<? super T> cmp) static <T> ObjIterator<T>
repeat
(T e, int n) static <T> ObjIterator<T>
repeatAll
(Collection<? extends T> c, int n) static <T> ObjIterator<T>
repeatAllToSize
(Collection<? extends T> c, int size) Repeat all to size.static <T> ObjIterator<T>
repeatEach
(Collection<? extends T> c, int n) static <T> ObjIterator<T>
repeatEachToSize
(Collection<? extends T> c, int size) Repeat each to size.static <T> ObjIterator<T>
Callsnext()
oniterator
, eithern
times or untilhasNext()
returnsfalse
, whichever comes first.static <T> ObjIterator<T>
skipAndLimit
(Iterator<? extends T> iter, long offset, long count) Callsnext()
oniterator
, eitheroffset
times or untilhasNext()
returnsfalse
, whichever comes first.static <T> ObjIterator<T>
Returns a newObjIterator
withnull
elements removed.static <T> ObjIterator<T>
static <T> ObjIterator<T>
static <T> ObjIterator<T>
takeWhileInclusive
(Iterator<? extends T> iter, Predicate<? super T> filter) static <T,
A, B> BiIterator<A, B> unzip
(Iterable<? extends T> c, BiConsumer<? super T, Pair<A, B>> unzip) static <T,
A, B> BiIterator<A, B> unzip
(Iterator<? extends T> iter, BiConsumer<? super T, Pair<A, B>> unzip) static <T,
A, B, C> TriIterator<A, B, C> unzipp
(Iterable<? extends T> c, BiConsumer<? super T, Triple<A, B, C>> unzip) static <T,
A, B, C> TriIterator<A, B, C> unzipp
(Iterator<? extends T> iter, BiConsumer<? super T, Triple<A, B, C>> unzip) static <A,
B, R> ObjIterator<R> zip
(Iterable<A> a, Iterable<B> b, A valueForNoneA, B valueForNoneB, BiFunction<? super A, ? super B, R> zipFunction) static <A,
B, R> ObjIterator<R> zip
(Iterable<A> a, Iterable<B> b, BiFunction<? super A, ? super B, R> zipFunction) static <A,
B, C, R> ObjIterator<R> zip
(Iterable<A> a, Iterable<B> b, Iterable<C> c, A valueForNoneA, B valueForNoneB, C valueForNoneC, TriFunction<? super A, ? super B, ? super C, R> zipFunction) static <A,
B, C, R> ObjIterator<R> zip
(Iterable<A> a, Iterable<B> b, Iterable<C> c, TriFunction<? super A, ? super B, ? super C, R> zipFunction) static <A,
B, R> ObjIterator<R> zip
(Iterator<A> a, Iterator<B> b, A valueForNoneA, B valueForNoneB, BiFunction<? super A, ? super B, R> zipFunction) static <A,
B, R> ObjIterator<R> zip
(Iterator<A> a, Iterator<B> b, BiFunction<? super A, ? super B, R> zipFunction) static <A,
B, C, R> ObjIterator<R> zip
(Iterator<A> a, Iterator<B> b, Iterator<C> c, A valueForNoneA, B valueForNoneB, C valueForNoneC, TriFunction<? super A, ? super B, ? super C, R> zipFunction) static <A,
B, C, R> ObjIterator<R> zip
(Iterator<A> a, Iterator<B> b, Iterator<C> c, TriFunction<? super A, ? super B, ? super C, R> zipFunction)
-
Method Details
-
get
- Type Parameters:
T
-- Parameters:
iter
-index
-- Returns:
-
count
- Parameters:
iter
-- Returns:
-
count
public static <T,E extends Exception> long count(Iterator<? extends T> iter, Throwables.Predicate<? super T, E> filter) throws E- Parameters:
iter
-filter
-- Returns:
- Throws:
E extends Exception
-
repeat
- Type Parameters:
T
-- Parameters:
e
-n
-- Returns:
-
repeatEach
- Type Parameters:
T
-- Parameters:
c
-n
-- Returns:
-
repeatAll
- Type Parameters:
T
-- Parameters:
c
-n
-- Returns:
-
repeatEachToSize
Repeat each to size.- Type Parameters:
T
-- Parameters:
c
-size
-- Returns:
-
repeatAllToSize
Repeat all to size.- Type Parameters:
T
-- Parameters:
c
-size
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Parameters:
a
-- Returns:
-
concat
- Type Parameters:
T
-- Parameters:
a
-- Returns:
-
concat
- Type Parameters:
T
-- Parameters:
a
-- Returns:
-
concat
- Type Parameters:
T
-- Parameters:
a
-- Returns:
-
concat
@SafeVarargs public static <K,V> ObjIterator<Map.Entry<K,V>> concat(Map<? extends K, ? extends V>... a) - Type Parameters:
T
-- Parameters:
a
-- Returns:
-
concat
- Type Parameters:
T
-- Parameters:
c
-- Returns:
-
concatIterables
- Type Parameters:
T
-- Parameters:
c
-- Returns:
-
concat
- Type Parameters:
A
-B
-- Parameters:
a
-- Returns:
-
concat
- Type Parameters:
A
-B
-C
-- Parameters:
a
-- Returns:
-
merge
public static <T> ObjIterator<T> merge(Iterator<? extends T> a, Iterator<? extends T> b, BiFunction<? super T, ? super T, MergeResult> nextSelector) - Type Parameters:
T
-- Parameters:
a
-b
-nextSelector
-- Returns:
-
merge
public static <T> ObjIterator<T> merge(Collection<? extends Iterator<? extends T>> c, BiFunction<? super T, ? super T, MergeResult> nextSelector) - Parameters:
c
-nextSelector
- first parameter is selected ifNth.FIRST
is returned, otherwise the second parameter is selected.- Returns:
-
merge
public static <T> ObjIterator<T> merge(Iterable<? extends T> a, Iterable<? extends T> b, BiFunction<? super T, ? super T, MergeResult> nextSelector) - Type Parameters:
T
-- Parameters:
a
-b
-nextSelector
-- Returns:
-
mergeCollections
@Deprecated public static <T> ObjIterator<T> mergeCollections(Collection<? extends Collection<? extends T>> collections, BiFunction<? super T, ? super T, MergeResult> nextSelector) Deprecated.replaced bymergeIterables(Collection, BiFunction)
- Type Parameters:
T
-- Parameters:
collections
-nextSelector
-- Returns:
-
mergeIterables
public static <T> ObjIterator<T> mergeIterables(Collection<? extends Iterable<? extends T>> iterables, BiFunction<? super T, ? super T, MergeResult> nextSelector) -
mergeSorted
public static <T extends Comparable> ObjIterator<T> mergeSorted(Iterator<? extends T> a, Iterator<? extends T> b) - Type Parameters:
T
-- Parameters:
a
- should be in non-descending order as this method does not sort its input.b
- should be in non-descending order as this method does not sort its input.- Returns:
-
mergeSorted
public static <T> ObjIterator<T> mergeSorted(Iterator<? extends T> a, Iterator<? extends T> b, Comparator<? super T> cmp) - Type Parameters:
T
-- Parameters:
a
- should be in non-descending order as this method does not sort its input.b
- should be in non-descending order as this method does not sort its input.cmp
-- Returns:
-
mergeSorted
public static <T extends Comparable> ObjIterator<T> mergeSorted(Iterable<? extends T> a, Iterable<? extends T> b) - Type Parameters:
T
-- Parameters:
a
- should be in non-descending order as this method does not sort its input.b
- should be in non-descending order as this method does not sort its input.- Returns:
-
mergeSorted
public static <T> ObjIterator<T> mergeSorted(Iterable<? extends T> a, Iterable<? extends T> b, Comparator<? super T> cmp) - Type Parameters:
T
-- Parameters:
a
- should be in non-descending order as this method does not sort its input.b
- should be in non-descending order as this method does not sort its input.cmp
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipR> (Iterable<A> a, Iterable<B> b, BiFunction<? super A, ? super B, R> zipFunction) - Type Parameters:
A
-B
-R
-- Parameters:
a
-b
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipR> (Iterator<A> a, Iterator<B> b, BiFunction<? super A, ? super B, R> zipFunction) - Type Parameters:
A
-B
-R
-- Parameters:
a
-b
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipC, R> (Iterable<A> a, Iterable<B> b, Iterable<C> c, TriFunction<? super A, ? super B, ? super C, R> zipFunction) - Type Parameters:
A
-B
-C
-R
-- Parameters:
a
-b
-c
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipC, R> (Iterator<A> a, Iterator<B> b, Iterator<C> c, TriFunction<? super A, ? super B, ? super C, R> zipFunction) - Type Parameters:
A
-B
-C
-R
-- Parameters:
a
-b
-c
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipR> (Iterable<A> a, Iterable<B> b, A valueForNoneA, B valueForNoneB, BiFunction<? super A, ? super B, R> zipFunction) - Type Parameters:
A
-B
-R
-- Parameters:
a
-b
-valueForNoneA
-valueForNoneB
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipR> (Iterator<A> a, Iterator<B> b, A valueForNoneA, B valueForNoneB, BiFunction<? super A, ? super B, R> zipFunction) - Type Parameters:
A
-B
-R
-- Parameters:
a
-b
-valueForNoneA
-valueForNoneB
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipC, R> (Iterable<A> a, Iterable<B> b, Iterable<C> c, A valueForNoneA, B valueForNoneB, C valueForNoneC, TriFunction<? super A, ? super B, ? super C, R> zipFunction) - Type Parameters:
A
-B
-C
-R
-- Parameters:
a
-b
-c
-valueForNoneA
-valueForNoneB
-valueForNoneC
-zipFunction
-- Returns:
-
zip
public static <A,B, ObjIterator<R> zipC, R> (Iterator<A> a, Iterator<B> b, Iterator<C> c, A valueForNoneA, B valueForNoneB, C valueForNoneC, TriFunction<? super A, ? super B, ? super C, R> zipFunction) - Type Parameters:
A
-B
-C
-R
-- Parameters:
a
-b
-c
-valueForNoneA
-valueForNoneB
-valueForNoneC
-zipFunction
-- Returns:
-
unzip
public static <T,A, BiIterator<A,B> B> unzip(Iterator<? extends T> iter, BiConsumer<? super T, Pair<A, B>> unzip) - Type Parameters:
T
-A
-B
-- Parameters:
iter
-unzip
- the second parameter is an output parameter.- Returns:
-
unzip
public static <T,A, BiIterator<A,B> B> unzip(Iterable<? extends T> c, BiConsumer<? super T, Pair<A, B>> unzip) - Type Parameters:
T
-A
-B
-- Parameters:
c
-unzip
- the second parameter is an output parameter.- Returns:
-
unzipp
public static <T,A, TriIterator<A,B, C> B, unzippC> (Iterator<? extends T> iter, BiConsumer<? super T, Triple<A, B, C>> unzip) - Type Parameters:
T
-A
-B
-C
-- Parameters:
iter
-unzip
- the second parameter is an output parameter.- Returns:
-
unzipp
public static <T,A, TriIterator<A,B, C> B, unzippC> (Iterable<? extends T> c, BiConsumer<? super T, Triple<A, B, C>> unzip) - Type Parameters:
T
-A
-B
-C
-- Parameters:
c
-unzip
- the second parameter is an output parameter.- Returns:
-
advance
Note: copied from Google Guava under Apache license v2
Callsnext()
oniterator
, eithernumberToAdvance
times or untilhasNext()
returnsfalse
, whichever comes first.- Parameters:
iterator
-numberToAdvance
-- Returns:
-
skip
Callsnext()
oniterator
, eithern
times or untilhasNext()
returnsfalse
, whichever comes first. This is a lazy evaluation operation. Theskip
action is only triggered whenIterator.hasNext()
orIterator.next()
is called.- Type Parameters:
T
-- Parameters:
iter
-n
-- Returns:
-
limit
Returns a newIterator
.- Type Parameters:
T
-- Parameters:
iter
-count
-- Returns:
-
skipAndLimit
Callsnext()
oniterator
, eitheroffset
times or untilhasNext()
returnsfalse
, whichever comes first. This is a lazy evaluation operation. Theskip
action is only triggered whenIterator.hasNext()
orIterator.next()
is called.- Type Parameters:
T
-- Parameters:
iter
-offset
-count
-- Returns:
-
skipNull
Returns a newObjIterator
withnull
elements removed.- Type Parameters:
T
-- Parameters:
iter
-- Returns:
-
distinct
-
distinctBy
public static <T> ObjIterator<T> distinctBy(Iterator<? extends T> iter, Function<? super T, ?> keyMapper) -
filter
- Type Parameters:
T
-- Parameters:
iter
-filter
-- Returns:
-
takeWhile
-
takeWhileInclusive
public static <T> ObjIterator<T> takeWhileInclusive(Iterator<? extends T> iter, Predicate<? super T> filter) -
dropWhile
-
skipUntil
-
map
- Type Parameters:
T
-U
-- Parameters:
iter
-mapper
-- Returns:
-
flatMap
public static <T,U> ObjIterator<U> flatMap(Iterator<? extends T> iter, Function<? super T, ? extends Collection<? extends U>> mapper) - Type Parameters:
T
-U
-- Parameters:
iter
-mapper
-- Returns:
-
flattMap
public static <T,U> ObjIterator<U> flattMap(Iterator<? extends T> iter, Function<? super T, ? extends U[]> mapper) -
generate
public static <T,U> ObjIterator<T> generate(U init, Predicate<? super U> hasNext, Function<? super U, T> supplier) - Type Parameters:
T
-U
-- Parameters:
init
-hasNext
-supplier
-- Returns:
-
generate
public static <T,U> ObjIterator<T> generate(U init, BiPredicate<? super U, T> hasNext, BiFunction<? super U, T, T> supplier) - Type Parameters:
T
-U
-- Parameters:
init
-hasNext
-supplier
-- Returns:
-
mergeIterables(Collection, BiFunction)