Top Description Constructors Methods
org.eclipse.xtext.xbase.lib

public Class IterableExtensions

extends Object
Class Inheritance
Annotations
@GwtCompatible
Imports
java.util.Collection, .Collections, .Comparator, .Iterator, .List, .ListIterator, .Map, .NoSuchElementException, .Set, .SortedSet, org.eclipse.xtext.xbase.lib.Functions.Function1, .Functions.Function2, .Procedures.Procedure1, .Procedures.Procedure2, org.eclipse.xtext.xbase.lib.internal.BooleanFunctionDelegate, .FunctionDelegate, com.google.common.annotations.Beta, .GwtCompatible, .GwtIncompatible, com.google.common.base.Predicates, com.google.common.collect.Iterables, .Lists, .Sets

This is an extension library for iterables.
Authors
Sven Efftinge - Initial contribution and API, Sebastian Zarnekow

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if this Iterable contains the specified element
contains
(Iterable<?>
the elements to test
iterable
,
Object
element whose presence in the Iterable is to be tested
o
)

Returns true if this Iterable contains the specified element.

public static <T> Iterable<T>

Returns:

an iterable without the first count elements. Never null.
drop
(final Iterable<T>
the iterable. May not be null.
iterable
,
final int
the number of elements that should be dropped.
count
)

Returns a view on this iterable that provides all elements except the first count entries.

public static <T> Iterable<T>

Returns:

the remaining elements after dropping
dropWhile
(final Iterable<? extends T>
the elements from which to drop. May not be null.
iterable
,
final Functions.Function1<? super T, Boolean>
the predicate which decides whether to keep dropping elements. May not be null.
predicate
)

Returns an Iterable containing all elements starting from the first element for which the drop-predicate returned false.

public static boolean

Returns:

true if the two iterables contain equal elements in the same order.
elementsEqual
(Iterable<?>
an iterable. May not be null.
iterable
,
Iterable<?>
an iterable. May not be null.
other
)

Determines whether two iterables contain equal elements in the same order.

public static <T> boolean

Returns:

true if one or more elements in iterable satisfy the predicate.
exists
(Iterable<T>
the iterable. May not be null.
iterable
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Returns true if one or more elements in iterable satisfy the predicate.

public static <T> Iterable<T>

Returns:

an iterable that contains only the elements that fulfill the predicate. Never null.
filter
(Iterable<T>
the unfiltered iterable. May not be null.
unfiltered
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Returns the elements of unfiltered that satisfy a predicate.

public static <T> Iterable<T>

Returns:

an unmodifiable iterable containing all elements of the original iterable that were of the requested type. Never null.
filter
(Iterable<?>
the unfiltered iterable. May not be null.
unfiltered
,
Class<T>
the type of elements desired
type
)

Returns all instances of class type in unfiltered.

public static <T> Iterable<T>

Returns:

an unmodifiable iterable containing all elements of the original iterable without any null references. Never null.
filterNull
(Iterable<T>
the unfiltered iterable. May not be null.
unfiltered
)

Returns a new iterable filtering any null references.

public static <T> T

Returns:

the first element in the iterable for which the given predicate returns true, returns null if no element matches the predicate or the iterable is empty.
findFirst
(Iterable<T>
the iterable. May not be null.
iterable
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Finds the first element in the given iterable that fulfills the predicate.

public static <T> T

Returns:

the last element in the iterable for which the given predicate returns true, returns null if no element matches the predicate or the iterable is empty.
findLast
(Iterable<T>
the iterable. May not be null.
iterable
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Finds the last element in the given iterable that fulfills the predicate.

public static <T, R> Iterable<R>

Returns:

an iterable that provides the result of the transformation. Never null.
flatMap
(Iterable<T>
the original iterable. May not be null.
original
,
Functions.Function1<? super T, ? extends Iterable<R>>
the transformation. May not be null and must not yield null.
transformation
)

Returns an iterable that performs the given transformation for each element of original when requested.

public static <T> Iterable<T>

Returns:

an iterable that provides the concatenated values of the input elements. Never null.
flatten
(Iterable<? extends Iterable<? extends T>>
the to be flattened iterables. May not be null.
inputs
)

Combines multiple iterables into a single iterable.

public static <T, R> R

Returns:

the last result of the applied combinator function or seed for the empty input.
fold
(Iterable<T>
the to-be-folded iterable. May not be null.
iterable
,
R
the initial value. May be null.
seed
,
Functions.Function2<? super R, ? super T, ? extends R>
the combinator function. May not be null.
function
)

Applies the combinator function to all elements of the iterable in turn and uses seed as the start value.

public static <T> boolean

Returns:

true if every element in iterable satisfies the predicate and also if there is no element.
forall
(Iterable<T>
the iterable. May not be null.
iterable
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Returns true if every element in iterable satisfies the predicate.

public static <T> void
forEach(Iterable<T>
the iterable. May not be null.
iterable
,
Procedures.Procedure1<? super T>
the procedure. May not be null.
procedure
)

Applies procedure for each element of the given iterable.

public static <T> void
forEach(Iterable<T>
the iterable. May not be null.
iterable
,
Procedures.Procedure2<? super T, ? super Integer>
the procedure. May not be null.
procedure
)

Applies procedure for each element of the given iterable.

public static <K, V> Map<K, List<V>>

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.
groupBy
(Iterable<? extends V>
the values to use when constructing the Map. May not be null.
values
,
Functions.Function1<? super V, ? extends K>
the function used to produce the key for each value. May not be null.
computeKeys
)

Returns a map for which the Map#values is a collection of lists, where the elements in the list will appear in the order as they appeared in the iterable.

public static <T> T

Returns:

the first element in the iterable or null.
head
(Iterable<T>
the iterable. May not be null.
iterable
)

Returns the first element in the given iterable or null if empty.

public static <A> Iterable<Pair<Integer, A>>

Returns:

the zipped result
indexed
(final Iterable<? extends A>
the elements. May not be null.
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.

public static boolean

Returns:

true if the iterable contains no elements
isEmpty
(Iterable<?>
the to-be-queried iterable. May not be null.
iterable
)

Determines if the given iterable contains no elements.

public static boolean

Returns:

true if the iterable is null or contains no elements
isNullOrEmpty
(Iterable<?>
the to-be-queried iterable. May be null.
iterable
)

Determines if the given iterable is null or contains no elements.

public static String

Returns:

the string representation of the iterable's elements. Never null.
join
(Iterable<?>
the iterable. May not be null.
iterable
)

Returns the concatenated string representation of the elements in the given iterable.

public static String

Returns:

the string representation of the iterable's elements. Never null.
join
(Iterable<?>
the iterable. May not be null.
iterable
,
CharSequence
the separator. May not be null.
separator
)

Returns the concatenated string representation of the elements in the given iterable.

public static <T> String

Returns:

the string representation of the iterable's elements. Never null.
join
(Iterable<T>
the iterable. May not be null.
iterable
,
CharSequence
the separator. May not be null.
separator
,
Functions.Function1<? super T, ? extends CharSequence>
the function that is used to compute the string representation of a single element. May not be null.
function
)

Returns the concatenated string representation of the elements in the given iterable.

public static <T> String

Returns:

the string representation of the iterable's elements. Never null.
join
(Iterable<T>
the iterable. May not be null.
iterable
,
CharSequence
prepends the resulting string if the iterable contains at least one element. May be null which is equivalent to passing an empty string.
before
,
CharSequence
the separator. May be null which is equivalent to passing an empty string.
separator
,
CharSequence
appended to the resulting string if the iterable contain at least one element. May be null which is equivalent to passing an empty string.
after
,
Functions.Function1<? super T, ? extends CharSequence>
the function that is used to compute the string representation of a single element. May not be null.
function
)

Returns the concatenated string representation of the elements in the given iterable.

public static <T> T

Returns:

the last element in the iterable or null.
last
(Iterable<T>
the iterable. May not be null.
iterable
)
Deprecated Use lastOrNull(Iterable<T>) instead: in Java 21, the method getLast has been introduced to some collection classes and would be preferred to this one by the Xbase/Xtend compiler, breaking the semantics of the original code: The Java 21 getLast method throws a NoSuchElementException when the collection is empty, instead of returning null.

Returns the last element in the given iterable or null if empty.

public static <T> T

Returns:

the last element in the iterable or null.
lastOrNull
(Iterable<T>
the iterable. May not be null.
iterable
)

Returns the last element in the given iterable or null if empty.

public static <T, R> Iterable<R>

Returns:

an iterable that provides the result of the transformation. Never null.
map
(Iterable<T>
the original iterable. May not be null.
original
,
Functions.Function1<? super T, ? extends R>
the transformation. May not be null.
transformation
)

Returns an iterable that performs the given transformation for each element of original when requested.

public static <T extends Comparable<? super T>> T

Returns:

the maximum
max
(final Iterable<T>
the mutually comparable elements. May not be null.
iterable
)

Finds the maximum of the elements according to their natural ordering.

public static <T> T

Returns:

the maximum
max
(final Iterable<T>
the elements to find the maximum of. May not be null.
iterable
,
Comparator<? super T>
the comparison function. May not be null.
comparator
)

Finds the maximum element according to comparator.

public static <T, C extends Comparable<? super C>> T

Returns:

the maximum
maxBy
(final Iterable<T>
the elements to find the maximum of. May not be null.
iterable
,
final Functions.Function1<? super T, C>
a function that returns a comparable characteristic to compare the elements by. May not be null.
compareBy
)

Finds the element that yields the maximum value when passed to compareBy If there are several maxima, the first one will be returned.

public static <T extends Comparable<? super T>> T

Returns:

the minimum
min
(final Iterable<T>
the mutually comparable elements. May not be null.
iterable
)

Finds the minimum of the given elements according to their natural ordering.

public static <T> T

Returns:

the minimum
min
(final Iterable<T>
the elements to find the minimum of. May not be null.
iterable
,
Comparator<? super T>
the comparison function. May not be null.
comparator
)

Finds the mininmum element according to comparator.

public static <T, C extends Comparable<? super C>> T

Returns:

the minimum
minBy
(final Iterable<T>
the elements to find the minimum of. May not be null.
iterable
,
final Functions.Function1<? super T, C>
a function that returns a comparable characteristic to compare the elements by. May not be null.
compareBy
)

Finds the element that yields the minimum value when passed to compareBy.

public static <T> Iterable<T>

Returns:

a combined iterable. Never null.
operator_plus
(Iterable<? extends T>
the first iterable. May not be null.
a
,
Iterable<? extends T>
the second iterable. May not be null.
b
)

Concatenates two iterables into a single iterable.

public static <T> T

Returns:

the last result of the applied combinator function or null for the empty input.
reduce
(Iterable<? extends T>
the to-be-reduced iterable. May not be null.
iterable
,
Functions.Function2<? super T, ? super T, ? extends T>
the combinator function. May not be null.
function
)

Applies the combinator function to all elements of the iterable in turn.

public static <T> Iterable<T>

Returns:

an iterable that contains only the elements that do not fulfill the predicate. Never null.
reject
(Iterable<T>
the unfiltered iterable. May not be null.
unfiltered
,
Functions.Function1<? super T, Boolean>
the predicate. May not be null.
predicate
)

Returns the elements of unfiltered that do not satisfy a predicate.

public static <T> Iterable<T>

Returns:

an iterable that contains only the elements that are not instances of type. Never null. Note that the elements of the iterable can be null as null is an instance of nothing.
reject
(Iterable<T>
the unfiltered iterable. May not be null.
unfiltered
,
Class<?>
the type of elements undesired. May not be null.
type
)

Returns the elements of unfiltered that are not instanceof type.

public static int

Returns:

the number of elements in iterable.
size
(Iterable<?>
the iterable. May not be null.
iterable
)

Returns the number of elements in iterable.

public static <T extends Comparable<? super T>> List<T>

Returns:

a sorted list as a shallow copy of the given iterable.
sort
(Iterable<T>
the items to be sorted. May not be null.
iterable
)

Creates a sorted list that contains the items of the given iterable.

public static <T> List<T>

Returns:

a sorted list as a shallow copy of the given iterable.
sort
(Iterable<T>
the items to be sorted. May not be null.
iterable
,
Comparator<? super T>
the comparator to be used. May be null to indicate that the natural ordering of the elements should be used.
comparator
)
Deprecated Use sortWith(Iterable, Comparator) instead.

This method is deprecated in favor of sortWith(Iterable, Comparator).

public static <T, C extends Comparable<? super C>> List<T>

Returns:

a sorted list as a shallow copy of the given iterable.
sortBy
(Iterable<T>
the elements to be sorted. May not be null.
iterable
,
final Functions.Function1<? super T, C>
the key function to-be-used. May not be null.
key
)

Creates a sorted list that contains the items of the given iterable.

public static <T> List<T>

Returns:

a sorted list as a shallow copy of the given iterable.
sortWith
(Iterable<T>
the items to be sorted. May not be null.
iterable
,
Comparator<? super T>
the comparator to be used. May be null to indicate that the natural ordering of the elements should be used.
comparator
)

Creates a sorted list that contains the items of the given iterable.

public static <T> Iterable<T>

Returns:

an iterable with all elements except the first. Never null.
tail
(final Iterable<T>
the iterable. May not be null.
iterable
)

Returns a view on this iterable that contains all the elements except the first.

public static <T> Iterable<T>

Returns:

an iterable with count elements. Never null.
take
(final Iterable<T>
the iterable. May not be null.
iterable
,
final int
the number of elements that should be returned at most.
count
)

Returns a view on this iterable that provides at most the first count entries.

public static <T> Iterable<T>

Returns:

the taken elements
takeWhile
(final Iterable<? extends T>
the elements from which to take. May not be null.
iterable
,
final Functions.Function1<? super T, Boolean>
the predicate which decides whether to keep taking elements. May not be null.
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.

public static <K, V> Map<K, V>

Returns:

a map mapping each entry in the given iterable to the corresponding result when evaluating the function computeValues.
toInvertedMap
(Iterable<? extends K>
the keys to use when constructing the Map. May not be null.
keys
,
Functions.Function1<? super K, V>
the function used to produce the values for each key. May not be null.
computeValues
)

Returns a map for which the Map#values are computed by the given function, and each key is an element in the given keys.

public static <T> List<T>

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. Never null.
toList
(Iterable<T>
the iterable. May not be null.
iterable
)

Returns a list that contains all the entries of the given iterable in the same order.

public static <K, V> Map<K, V>

Returns:

a map mapping the result of evaluating the function keyFunction on each value in the input collection to that value
toMap
(Iterable<? extends V>
the values to use when constructing the Map. May not be null.
values
,
Functions.Function1<? super V, K>
the function used to produce the key for each value. May not be null.
computeKeys
)

Returns a map for which the Map#values are the given elements in the given order, and each key is the product of invoking a supplied function computeKeys on its corresponding value.

public static <T, K, V> Map<K, V>

Returns:

a map mapping the result of evaluating the functions keyFunction and computeValues on each value in the input iterator to that value
toMap
(Iterable<? extends T>
the elements to use when constructing the Map. May not be null.
inputs
,
Functions.Function1<? super T, K>
the function used to produce the key for each value. May not be null.
computeKeys
,
Functions.Function1<? super T, V>
the function used to produce the values for each key. May not be null.
computeValues
)

Returns a map for which the Map#values are the product of invoking supplied function computeValues on input iterable elements, and each key is the product of invoking a supplied function computeKeys on same elements.

public static <T> Set<T>

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. Never null.
toSet
(Iterable<T>
the iterable. May not be null.
iterable
)

Returns a set that contains all the unique entries of the given iterable in the order of their appearance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait