Class IterableExtensions
- java.lang.Object
-
- org.eclipse.xtext.xbase.lib.IterableExtensions
-
@GwtCompatible public class IterableExtensions extends java.lang.Object
This is an extension library foriterables
.
-
-
Constructor Summary
Constructors Constructor Description IterableExtensions()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static boolean
contains(java.lang.Iterable<?> iterable, java.lang.Object o)
Returns true if this Iterable contains the specified element.static <T> java.lang.Iterable<T>
drop(java.lang.Iterable<T> iterable, int count)
Returns a view on this iterable that provides all elements except the firstcount
entries.static <T> java.lang.Iterable<T>
dropWhile(java.lang.Iterable<? extends T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterable containing all elements starting from the first element for which the drop-predicate returned false.static boolean
elementsEqual(java.lang.Iterable<?> iterable, java.lang.Iterable<?> other)
Determines whether two iterables contain equal elements in the same order.static <T> boolean
exists(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returnstrue
if one or more elements initerable
satisfy the predicate.static <T> java.lang.Iterable<T>
filter(java.lang.Iterable<?> unfiltered, java.lang.Class<T> type)
Returns all instances of classtype
inunfiltered
.static <T> java.lang.Iterable<T>
filter(java.lang.Iterable<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements ofunfiltered
that satisfy a predicate.static <T> java.lang.Iterable<T>
filterNull(java.lang.Iterable<T> unfiltered)
Returns a new iterable filtering any null references.static <T> T
findFirst(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the first element in the given iterable that fulfills the predicate.static <T> T
findLast(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the last element in the given iterable that fulfills the predicate.static <T,R>
java.lang.Iterable<R>flatMap(java.lang.Iterable<T> original, Functions.Function1<? super T,? extends java.lang.Iterable<R>> transformation)
Returns an iterable that performs the giventransformation
for each element oforiginal
when requested.static <T> java.lang.Iterable<T>
flatten(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> inputs)
Combines multiple iterables into a single iterable.static <T,R>
Rfold(java.lang.Iterable<T> iterable, R seed, Functions.Function2<? super R,? super T,? extends R> function)
Applies the combinatorfunction
to all elements of the iterable in turn and usesseed
as the start value.static <T> boolean
forall(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returnstrue
if every element initerable
satisfies the predicate.static <T> void
forEach(java.lang.Iterable<T> iterable, Procedures.Procedure1<? super T> procedure)
Appliesprocedure
for each element of the given iterable.static <T> void
forEach(java.lang.Iterable<T> iterable, Procedures.Procedure2<? super T,? super java.lang.Integer> procedure)
Appliesprocedure
for each element of the given iterable.static <K,V>
java.util.Map<K,java.util.List<V>>groupBy(java.lang.Iterable<? extends V> values, Functions.Function1<? super V,? extends K> computeKeys)
Returns a map for which theMap.values()
is a collection of lists, where the elements in the list will appear in the order as they appeared in the iterable.static <T> T
head(java.lang.Iterable<T> iterable)
Returns the first element in the given iterable ornull
if empty.static <A> java.lang.Iterable<Pair<java.lang.Integer,A>>
indexed(java.lang.Iterable<? extends A> iterable)
Returns an Iterable of Pairs where the nth pair is created by taking the nth element of the source as the value its 0-based index as the key.static boolean
isEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable contains no elements.static boolean
isNullOrEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable isnull
or contains no elements.static java.lang.String
join(java.lang.Iterable<?> iterable)
Returns the concatenated string representation of the elements in the given iterable.static java.lang.String
join(java.lang.Iterable<?> iterable, java.lang.CharSequence separator)
Returns the concatenated string representation of the elements in the given iterable.static <T> java.lang.String
join(java.lang.Iterable<T> iterable, java.lang.CharSequence before, java.lang.CharSequence separator, java.lang.CharSequence after, Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable.static <T> java.lang.String
join(java.lang.Iterable<T> iterable, java.lang.CharSequence separator, Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable.static <T> T
last(java.lang.Iterable<T> iterable)
Returns the last element in the given iterable ornull
if empty.static <T,R>
java.lang.Iterable<R>map(java.lang.Iterable<T> original, Functions.Function1<? super T,? extends R> transformation)
Returns an iterable that performs the giventransformation
for each element oforiginal
when requested.static <T extends java.lang.Comparable<? super T>>
Tmax(java.lang.Iterable<T> iterable)
Finds the maximum of the elements according to their natural ordering.static <T> T
max(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Finds the maximum element according tocomparator
.static <T,C extends java.lang.Comparable<? super C>>
TmaxBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> compareBy)
Finds the element that yields the maximum value when passed tocompareBy
If there are several maxima, the first one will be returned.static <T extends java.lang.Comparable<? super T>>
Tmin(java.lang.Iterable<T> iterable)
Finds the minimum of the given elements according to their natural ordering.static <T> T
min(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Finds the mininmum element according tocomparator
.static <T,C extends java.lang.Comparable<? super C>>
TminBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> compareBy)
Finds the element that yields the minimum value when passed tocompareBy
.static <T> java.lang.Iterable<T>
operator_plus(java.lang.Iterable<? extends T> a, java.lang.Iterable<? extends T> b)
Concatenates two iterables into a single iterable.static <T> T
reduce(java.lang.Iterable<? extends T> iterable, Functions.Function2<? super T,? super T,? extends T> function)
Applies the combinatorfunction
to all elements of the iterable in turn.static <T> java.lang.Iterable<T>
reject(java.lang.Iterable<T> unfiltered, java.lang.Class<?> type)
Returns the elements ofunfiltered
that are not instanceoftype
.static <T> java.lang.Iterable<T>
reject(java.lang.Iterable<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements ofunfiltered
that do not satisfy a predicate.static int
size(java.lang.Iterable<?> iterable)
Returns the number of elements initerable
.static <T extends java.lang.Comparable<? super T>>
java.util.List<T>sort(java.lang.Iterable<T> iterable)
Creates a sorted list that contains the items of the given iterable.static <T> java.util.List<T>
sort(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Deprecated.UsesortWith(Iterable, Comparator)
instead.static <T,C extends java.lang.Comparable<? super C>>
java.util.List<T>sortBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> key)
Creates a sorted list that contains the items of the given iterable.static <T> java.util.List<T>
sortWith(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Creates a sorted list that contains the items of the given iterable.static <T> java.lang.Iterable<T>
tail(java.lang.Iterable<T> iterable)
Returns a view on this iterable that contains all the elements except the first.static <T> java.lang.Iterable<T>
take(java.lang.Iterable<T> iterable, int count)
Returns a view on this iterable that provides at most the firstcount
entries.static <T> java.lang.Iterable<T>
takeWhile(java.lang.Iterable<? extends T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterable containing all elements starting from the head of the source up to and excluding the first element that violates the predicate The resulting Iterable is a lazily computed view, so any modifications to the underlying Iterables will be reflected on subsequent iterations.static <K,V>
java.util.Map<K,V>toInvertedMap(java.lang.Iterable<? extends K> keys, Functions.Function1<? super K,V> computeValues)
Returns a map for which theMap.values()
are computed by the given function, and each key is an element in the givenkeys
.static <T> java.util.List<T>
toList(java.lang.Iterable<T> iterable)
Returns a list that contains all the entries of the given iterable in the same order.static <T,K,V>
java.util.Map<K,V>toMap(java.lang.Iterable<? extends T> inputs, Functions.Function1<? super T,K> computeKeys, Functions.Function1<? super T,V> computeValues)
Returns a map for which theMap.values()
are the product of invoking supplied functioncomputeValues
on input iterable elements, and each key is the product of invoking a supplied functioncomputeKeys
on same elements.static <K,V>
java.util.Map<K,V>toMap(java.lang.Iterable<? extends V> values, Functions.Function1<? super V,K> computeKeys)
Returns a map for which theMap.values()
are the given elements in the given order, and each key is the product of invoking a supplied functioncomputeKeys
on its corresponding value.static <T> java.util.Set<T>
toSet(java.lang.Iterable<T> iterable)
Returns a set that contains all the unique entries of the given iterable in the order of their appearance.
-
-
-
Method Detail
-
operator_plus
@Pure public static <T> java.lang.Iterable<T> operator_plus(java.lang.Iterable<? extends T> a, java.lang.Iterable<? extends T> b)
Concatenates two iterables into a single iterable. The returned iterable has an iterator that traverses the elements in
a
, followed by the elements inb
. The resulting iterable is effectivly a view on the source iterables. That is, the source iterators are not polled until necessary and the result will reflect changes in the sources.The returned iterable's iterator supports
remove()
when the corresponding input iterator supports it.- Parameters:
a
- the first iterable. May not benull
.b
- the second iterable. May not benull
.- Returns:
- a combined iterable. Never
null
.
-
findFirst
public static <T> T findFirst(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the first element in the given iterable that fulfills the predicate. If none is found or the iterable is empty,null
is returned.- Parameters:
iterable
- the iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
- the first element in the iterable for which the given predicate returns
true
, returnsnull
if no element matches the predicate or the iterable is empty.
-
findLast
public static <T> T findLast(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the last element in the given iterable that fulfills the predicate. If none is found or the iterable is empty,null
is returned.- Parameters:
iterable
- the iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
- the last element in the iterable for which the given predicate returns
true
, returnsnull
if no element matches the predicate or the iterable is empty.
-
head
public static <T> T head(java.lang.Iterable<T> iterable)
Returns the first element in the given iterable ornull
if empty.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- the first element in the iterable or
null
.
-
tail
public static <T> java.lang.Iterable<T> tail(java.lang.Iterable<T> iterable)
Returns a view on this iterable that contains all the elements except the first.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- an iterable with all elements except the first. Never
null
. - See Also:
drop(Iterable, int)
-
last
public static <T> T last(java.lang.Iterable<T> iterable)
Returns the last element in the given iterable ornull
if empty.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- the last element in the iterable or
null
.
-
take
public static <T> java.lang.Iterable<T> take(java.lang.Iterable<T> iterable, int count)
Returns a view on this iterable that provides at most the firstcount
entries.- Parameters:
iterable
- the iterable. May not benull
.count
- the number of elements that should be returned at most.- Returns:
- an iterable with
count
elements. Nevernull
. - Throws:
java.lang.IllegalArgumentException
- ifcount
is negative.
-
drop
@Pure public static <T> java.lang.Iterable<T> drop(java.lang.Iterable<T> iterable, int count)
Returns a view on this iterable that provides all elements except the firstcount
entries.- Parameters:
iterable
- the iterable. May not benull
.count
- the number of elements that should be dropped.- Returns:
- an iterable without the first
count
elements. Nevernull
. - Throws:
java.lang.IllegalArgumentException
- ifcount
is negative.
-
exists
public static <T> boolean exists(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returnstrue
if one or more elements initerable
satisfy the predicate.- Parameters:
iterable
- the iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
true
if one or more elements initerable
satisfy the predicate.
-
forall
public static <T> boolean forall(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returnstrue
if every element initerable
satisfies the predicate. Ifiterable
is empty,true
is returned. In other words,false
is returned if at least one element fails to fulfill the predicate.- Parameters:
iterable
- the iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
true
if every element initerable
satisfies the predicate and also if there is no element.
-
filter
@Pure public static <T> java.lang.Iterable<T> filter(java.lang.Iterable<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements ofunfiltered
that satisfy a predicate. The resulting iterable's iterator does not supportremove()
. The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered
- the unfiltered iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
- an iterable that contains only the elements that fulfill the predicate. Never
null
.
-
reject
@Pure public static <T> java.lang.Iterable<T> reject(java.lang.Iterable<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements ofunfiltered
that do not satisfy a predicate. The resulting iterable's iterator does not supportremove()
. The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered
- the unfiltered iterable. May not benull
.predicate
- the predicate. May not benull
.- Returns:
- an iterable that contains only the elements that do not fulfill the predicate. Never
null
. - Since:
- 2.11
-
reject
@GwtIncompatible("Class.isInstance") @Pure public static <T> java.lang.Iterable<T> reject(java.lang.Iterable<T> unfiltered, java.lang.Class<?> type)
Returns the elements ofunfiltered
that are not instanceoftype
. The resulting iterable's iterator does not supportremove()
. The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered
- the unfiltered iterable. May not benull
.type
- the type of elements undesired. May not benull
.- Returns:
- an iterable that contains only the elements that are not instances of
type
. Nevernull
. Note that the elements of the iterable can be null as null is an instance of nothing. - Since:
- 2.15
-
filter
@GwtIncompatible("Class.isInstance") @Pure public static <T> java.lang.Iterable<T> filter(java.lang.Iterable<?> unfiltered, java.lang.Class<T> type)
Returns all instances of classtype
inunfiltered
. The returned iterable has elements whose class istype
or a subclass oftype
. The returned iterable's iterator does not supportremove()
. The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.- Parameters:
unfiltered
- the unfiltered iterable. May not benull
.type
- the type of elements desired- Returns:
- an unmodifiable iterable containing all elements of the original iterable that were of the requested
type. Never
null
.
-
filterNull
@Pure public static <T> java.lang.Iterable<T> filterNull(java.lang.Iterable<T> unfiltered)
Returns a new iterable filtering any null references.- Parameters:
unfiltered
- the unfiltered iterable. May not benull
.- Returns:
- an unmodifiable iterable containing all elements of the original iterable without any
null
references. Nevernull
.
-
map
@Pure public static <T,R> java.lang.Iterable<R> map(java.lang.Iterable<T> original, Functions.Function1<? super T,? extends R> transformation)
Returns an iterable that performs the giventransformation
for each element oforiginal
when requested. The mapping is done lazily. That is, subsequent iterations of the elements in the iterable will repeatedly apply the transformation.The returned iterable's iterator supports
remove()
if the provided iterator does. After a successfulremove()
call,original
no longer contains the corresponding element.- Parameters:
original
- the original iterable. May not benull
.transformation
- the transformation. May not benull
.- Returns:
- an iterable that provides the result of the transformation. Never
null
.
-
flatMap
@Pure public static <T,R> java.lang.Iterable<R> flatMap(java.lang.Iterable<T> original, Functions.Function1<? super T,? extends java.lang.Iterable<R>> transformation)
Returns an iterable that performs the giventransformation
for each element oforiginal
when requested. The mapping is done lazily. That is, subsequent iterations of the elements in the iterable will repeatedly apply the transformation.The transformation maps each element to an iterable, and all resulting iterables are combined to a single iterable. Effectively a combination of
map(Iterable, Functions.Function1)
andflatten(Iterable)
is performed.The returned iterable's iterator does not support
remove()
in contrast tomap(Iterable, Functions.Function1)
.- Parameters:
original
- the original iterable. May not benull
.transformation
- the transformation. May not benull
and must not yieldnull
.- Returns:
- an iterable that provides the result of the transformation. Never
null
. - Since:
- 2.13
-
flatten
public static <T> java.lang.Iterable<T> flatten(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> inputs)
Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements of each iterable ininputs
. The input iterators are not polled until necessary.The returned iterable's iterator supports
remove()
when the corresponding input iterator supports it. The methods of the returned iterable may throwNullPointerException
if any of the input iterators are null.- Parameters:
inputs
- the to be flattened iterables. May not benull
.- Returns:
- an iterable that provides the concatenated values of the input elements. Never
null
.
-
forEach
public static <T> void forEach(java.lang.Iterable<T> iterable, Procedures.Procedure1<? super T> procedure)
Appliesprocedure
for each element of the given iterable.- Parameters:
iterable
- the iterable. May not benull
.procedure
- the procedure. May not benull
.
-
forEach
public static <T> void forEach(java.lang.Iterable<T> iterable, Procedures.Procedure2<? super T,? super java.lang.Integer> procedure)
Appliesprocedure
for each element of the given iterable. The procedure takes the element and a loop counter. If the counter would overflow,Integer.MAX_VALUE
is returned for all subsequent elements. The first element is at index zero.- Parameters:
iterable
- the iterable. May not benull
.procedure
- the procedure. May not benull
.- Since:
- 2.3
-
join
public static java.lang.String join(java.lang.Iterable<?> iterable)
Returns the concatenated string representation of the elements in the given iterable. No delimiter is used.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- the string representation of the iterable's elements. Never
null
. - See Also:
join(Iterable, CharSequence, org.eclipse.xtext.xbase.lib.Functions.Function1)
-
join
public static java.lang.String join(java.lang.Iterable<?> iterable, java.lang.CharSequence separator)
Returns the concatenated string representation of the elements in the given iterable. Theseparator
is used to between each pair of entries in the input. The stringnull
is used fornull
entries in the input.- Parameters:
iterable
- the iterable. May not benull
.separator
- the separator. May not benull
.- Returns:
- the string representation of the iterable's elements. Never
null
. - See Also:
join(Iterable, CharSequence, org.eclipse.xtext.xbase.lib.Functions.Function1)
-
join
public static <T> java.lang.String join(java.lang.Iterable<T> iterable, java.lang.CharSequence separator, Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable. Thefunction
is used to compute the string for each element. Theseparator
is used to between each pair of entries in the input. The stringnull
is used if the function yieldsnull
as the string representation for an entry.- Parameters:
iterable
- the iterable. May not benull
.separator
- the separator. May not benull
.function
- the function that is used to compute the string representation of a single element. May not benull
.- Returns:
- the string representation of the iterable's elements. Never
null
.
-
join
public static <T> java.lang.String join(java.lang.Iterable<T> iterable, java.lang.CharSequence before, java.lang.CharSequence separator, java.lang.CharSequence after, Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable. Thefunction
is used to compute the string for each element. Theseparator
is used to between each pair of entries in the input. The stringnull
is used if the function yieldsnull
as the string representation for an entry.- Parameters:
iterable
- the iterable. May not benull
.before
- prepends the resulting string if the iterable contains at least one element. May benull
which is equivalent to passing an empty string.separator
- the separator. May benull
which is equivalent to passing an empty string.after
- appended to the resulting string if the iterable contain at least one element. May benull
which is equivalent to passing an empty string.function
- the function that is used to compute the string representation of a single element. May not benull
.- Returns:
- the string representation of the iterable's elements. Never
null
.
-
elementsEqual
public static boolean elementsEqual(java.lang.Iterable<?> iterable, java.lang.Iterable<?> other)
Determines whether two iterables contain equal elements in the same order. More specifically, this method returnstrue
ifiterable
andother
contain the same number of elements and every element ofiterable
is equal to the corresponding element ofother
.- Parameters:
iterable
- an iterable. May not benull
.other
- an iterable. May not benull
.- Returns:
true
if the two iterables contain equal elements in the same order.
-
isNullOrEmpty
public static boolean isNullOrEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable isnull
or contains no elements.- Parameters:
iterable
- the to-be-queried iterable. May benull
.- Returns:
true
if the iterable isnull
or contains no elements
-
isEmpty
public static boolean isEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable contains no elements.- Parameters:
iterable
- the to-be-queried iterable. May not benull
.- Returns:
true
if the iterable contains no elements- See Also:
isNullOrEmpty(Iterable)
-
size
public static int size(java.lang.Iterable<?> iterable)
Returns the number of elements initerable
.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- the number of elements in
iterable
.
-
reduce
public static <T> T reduce(java.lang.Iterable<? extends T> iterable, Functions.Function2<? super T,? super T,? extends T> function)
Applies the combinator
function
to all elements of the iterable in turn.One of the function parameters is an element of the iterable, and the other is the result of previous application of the function. The seed of the operation is the first element in the iterable. The second value is computed by applying the function to the seed together with the second element of the iterable. The third value is computed from the previous result together with the third element and so on. In other words, the previous result of each step is taken and passed together with the next element to the combinator function.
If the iterable is empty,
null
is returned.More formally, given an iterable
[a, b, c, d]
and a functionf
, the result ofreduce
isf(f(f(a, b), c), d)
- Parameters:
iterable
- the to-be-reduced iterable. May not benull
.function
- the combinator function. May not benull
.- Returns:
- the last result of the applied combinator function or
null
for the empty input.
-
fold
public static <T,R> R fold(java.lang.Iterable<T> iterable, R seed, Functions.Function2<? super R,? super T,? extends R> function)
Applies the combinator
function
to all elements of the iterable in turn and usesseed
as the start value.One of the function parameters is an element of the iterable, and the other is the result of previous application of the function. The seed of the operation is explicitly passed to
fold
. The first computed value is the result of the applied function forseed
and the first element of the iterable. This intermediate result together with the second element of the iterable produced the next result and so on.fold
is similar toreduce
but allows aseed
value and the combinatorfunction
may be asymmetric. It takesT and R
and returnsR
.If the iterable is empty,
seed
is returned.More formally, given an iterable
[a, b, c, d]
, a seedinitial
and a functionf
, the result offold
isf(f(f(f(initial, a), b), c), d)
- Parameters:
iterable
- the to-be-folded iterable. May not benull
.seed
- the initial value. May benull
.function
- the combinator function. May not benull
.- Returns:
- the last result of the applied combinator function or
seed
for the empty input.
-
toList
@Beta public static <T> java.util.List<T> toList(java.lang.Iterable<T> iterable)
Returns a list that contains all the entries of the given iterable in the same order. If the iterable is of typeList
, itself is returned. Therefore an unchecked cast is performed. In all other cases, the result list is a copy of the iterable.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- a list with the same entries as the given iterable. May be the same as the given iterable iff it
implements
List
, otherwise a copy is returned. Nevernull
.
-
toSet
@Beta public static <T> java.util.Set<T> toSet(java.lang.Iterable<T> iterable)
Returns a set that contains all the unique entries of the given iterable in the order of their appearance. If the iterable is of typeSet
, itself is returned. Therefore an unchecked cast is performed. In all other cases, the result set is a copy of the iterable with stable order.- Parameters:
iterable
- the iterable. May not benull
.- Returns:
- a set with the unique entries of the given iterable. May be the same as the given iterable iff it
implements
Set
, otherwise a copy is returned. Nevernull
.
-
toInvertedMap
public static <K,V> java.util.Map<K,V> toInvertedMap(java.lang.Iterable<? extends K> keys, Functions.Function1<? super K,V> computeValues)
Returns a map for which theMap.values()
are computed by the given function, and each key is an element in the givenkeys
. If the iterable contains equal keys more than once, the last one will be contained in the map. The map is computed eagerly. That is, subsequent changes in the keys are not reflected by the map.- Parameters:
keys
- the keys to use when constructing theMap
. May not benull
.computeValues
- the function used to produce the values for each key. May not benull
.- Returns:
- a map mapping each entry in the given iterable to the corresponding result when evaluating the function
computeValues
.
-
toMap
public static <K,V> java.util.Map<K,V> toMap(java.lang.Iterable<? extends V> values, Functions.Function1<? super V,K> computeKeys)
Returns a map for which theMap.values()
are the given elements in the given order, and each key is the product of invoking a supplied functioncomputeKeys
on its corresponding value. If the function produces the same key for different values, the last one will be contained in the map.- Parameters:
values
- the values to use when constructing theMap
. May not benull
.computeKeys
- the function used to produce the key for each value. May not benull
.- Returns:
- a map mapping the result of evaluating the function
keyFunction
on each value in the input collection to that value
-
toMap
public static <T,K,V> java.util.Map<K,V> toMap(java.lang.Iterable<? extends T> inputs, Functions.Function1<? super T,K> computeKeys, Functions.Function1<? super T,V> computeValues)
Returns a map for which theMap.values()
are the product of invoking supplied functioncomputeValues
on input iterable elements, and each key is the product of invoking a supplied functioncomputeKeys
on same elements. If the function produces the same key for different values, the last one will be contained in the map.- Parameters:
inputs
- the elements to use when constructing theMap
. May not benull
.computeKeys
- the function used to produce the key for each value. May not benull
.computeValues
- the function used to produce the values for each key. May not benull
.- Returns:
- a map mapping the result of evaluating the functions
keyFunction
andcomputeValues
on each value in the input iterator to that value
-
groupBy
public static <K,V> java.util.Map<K,java.util.List<V>> groupBy(java.lang.Iterable<? extends V> values, Functions.Function1<? super V,? extends K> computeKeys)
Returns a map for which theMap.values()
is a collection of lists, where the elements in the list will appear in the order as they appeared in the iterable. Each key is the product of invoking the supplied functioncomputeKeys
on its corresponding value. So a key of that map groups a list of values for which the function produced exactly that key.- Parameters:
values
- the values to use when constructing theMap
. May not benull
.computeKeys
- the function used to produce the key for each value. May not benull
.- Returns:
- a map mapping the result of evaluating the function
keyFunction
on each value in the input iterable to that value. As there can be more than one value mapped by a key, the mapping result is is a list of values. - Since:
- 2.7
-
sort
public static <T extends java.lang.Comparable<? super T>> java.util.List<T> sort(java.lang.Iterable<T> iterable)
Creates a sorted list that contains the items of the given iterable. The resulting list is in ascending order, according to the natural ordering of the elements in the iterable.- Parameters:
iterable
- the items to be sorted. May not benull
.- Returns:
- a sorted list as a shallow copy of the given iterable.
- See Also:
Collections.sort(List)
,sort(Iterable, Comparator)
,sortBy(Iterable, org.eclipse.xtext.xbase.lib.Functions.Function1)
,ListExtensions.sortInplace(List)
-
sort
@Deprecated public static <T> java.util.List<T> sort(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Deprecated.UsesortWith(Iterable, Comparator)
instead.This method is deprecated in favor ofsortWith(Iterable, Comparator)
. Creates a sorted list that contains the items of the given iterable. The resulting list is sorted according to the order induced by the specified comparator.- Parameters:
iterable
- the items to be sorted. May not benull
.comparator
- the comparator to be used. May benull
to indicate that the natural ordering of the elements should be used.- Returns:
- a sorted list as a shallow copy of the given iterable.
- See Also:
sortWith(Iterable, Comparator)
-
sortWith
public static <T> java.util.List<T> sortWith(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Creates a sorted list that contains the items of the given iterable. The resulting list is sorted according to the order induced by the specified comparator.- Parameters:
iterable
- the items to be sorted. May not benull
.comparator
- the comparator to be used. May benull
to indicate that the natural ordering of the elements should be used.- Returns:
- a sorted list as a shallow copy of the given iterable.
- Since:
- 2.7
- See Also:
Collections.sort(List, Comparator)
,sort(Iterable)
,sortBy(Iterable, org.eclipse.xtext.xbase.lib.Functions.Function1)
,ListExtensions.sortInplace(List, Comparator)
-
sortBy
public static <T,C extends java.lang.Comparable<? super C>> java.util.List<T> sortBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> key)
Creates a sorted list that contains the items of the given iterable. The resulting list is sorted according to the order induced by applying a key function to each element which yields a comparable criteria.- Parameters:
iterable
- the elements to be sorted. May not benull
.key
- the key function to-be-used. May not benull
.- Returns:
- a sorted list as a shallow copy of the given iterable.
- See Also:
sort(Iterable)
,sort(Iterable, Comparator)
,ListExtensions.sortInplaceBy(List, org.eclipse.xtext.xbase.lib.Functions.Function1)
-
takeWhile
public static <T> java.lang.Iterable<T> takeWhile(java.lang.Iterable<? extends T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterable containing all elements starting from the head of the source up to and excluding the first element that violates the predicate The resulting Iterable is a lazily computed view, so any modifications to the underlying Iterables will be reflected on subsequent iterations. The result's Iterator does not supportIterator.remove()
- Parameters:
iterable
- the elements from which to take. May not benull
.predicate
- the predicate which decides whether to keep taking elements. May not benull
.- Returns:
- the taken elements
- Since:
- 2.7
-
dropWhile
public static <T> java.lang.Iterable<T> dropWhile(java.lang.Iterable<? extends T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterable containing all elements starting from the first element for which the drop-predicate returned false. The resulting Iterable is a lazily computed view, so any modifications to the underlying Iterables will be reflected on subsequent iterations. The result's Iterator does not supportIterator.remove()
- Parameters:
iterable
- the elements from which to drop. May not benull
.predicate
- the predicate which decides whether to keep dropping elements. May not benull
.- Returns:
- the remaining elements after dropping
- Since:
- 2.7
-
indexed
public static <A> java.lang.Iterable<Pair<java.lang.Integer,A>> indexed(java.lang.Iterable<? extends A> iterable)
Returns an Iterable of Pairs where the nth pair is created by taking the nth element of the source as the value its 0-based index as the key. E.g.zipWitIndex(#["a", "b", "c"]) == #[(0, "a"), (1, "b"), (2, "c")]
If the index would overflow,Integer.MAX_VALUE
is returned for all subsequent elements. The resulting Iterable is a lazily computed view, so any modifications to the underlying Iterable will be reflected on iteration. The result's Iterator does not supportIterator.remove()
- Parameters:
iterable
- the elements. May not benull
.- Returns:
- the zipped result
- Since:
- 2.7
-
min
public static <T extends java.lang.Comparable<? super T>> T min(java.lang.Iterable<T> iterable)
Finds the minimum of the given elements according to their natural ordering. If there are several mimina, the first one will be returned.- Parameters:
iterable
- the mutually comparable elements. May not benull
.- Returns:
- the minimum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
minBy
public static <T,C extends java.lang.Comparable<? super C>> T minBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> compareBy)
Finds the element that yields the minimum value when passed tocompareBy
. If there are several minima, the first one will be returned.- Parameters:
iterable
- the elements to find the minimum of. May not benull
.compareBy
- a function that returns a comparable characteristic to compare the elements by. May not benull
.- Returns:
- the minimum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
min
public static <T> T min(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Finds the mininmum element according tocomparator
. If there are several minima, the first one will be returned.- Parameters:
iterable
- the elements to find the minimum of. May not benull
.comparator
- the comparison function. May not benull
.- Returns:
- the minimum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
max
public static <T extends java.lang.Comparable<? super T>> T max(java.lang.Iterable<T> iterable)
Finds the maximum of the elements according to their natural ordering. If there are several maxima, the first one will be returned.- Parameters:
iterable
- the mutually comparable elements. May not benull
.- Returns:
- the maximum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
maxBy
public static <T,C extends java.lang.Comparable<? super C>> T maxBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> compareBy)
Finds the element that yields the maximum value when passed tocompareBy
If there are several maxima, the first one will be returned.- Parameters:
iterable
- the elements to find the maximum of. May not benull
.compareBy
- a function that returns a comparable characteristic to compare the elements by. May not benull
.- Returns:
- the maximum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
max
public static <T> T max(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
Finds the maximum element according tocomparator
. If there are several maxima, the first one will be returned.- Parameters:
iterable
- the elements to find the maximum of. May not benull
.comparator
- the comparison function. May not benull
.- Returns:
- the maximum
- Throws:
java.util.NoSuchElementException
- if the iterable is empty- Since:
- 2.7
-
contains
public static boolean contains(java.lang.Iterable<?> iterable, java.lang.Object o)
Returns true if this Iterable contains the specified element. More formally, returns true if and only if this Iterable contains at least one element e such that (o==null ? e==null : o.equals(e)).- Parameters:
iterable
- the elements to testo
- element whose presence in the Iterable is to be tested- Returns:
- true if this Iterable contains the specified element
-
-