Enumerable<T> class
The base class for all enumerables. Allows advanced enumeration methods to be performed on the underlying collection.
Enumeration methods are separated into two categories: value methods and lazy-execution methods.
Value methods are methods that condense the enumerable into a single static value. These methods will iterate over the enumerable when the method is called, and may iterate over the entire enumerable (possibly more than once) or may short-circuit the iteration after certain conditions have been met. Examples of value methods include Aggregate, Max, and Count.
Lazy-execution methods are methods that modify the enumeration using a particular tactic. These methods do not execute until the enumeration is iterated over, which can drastically improve performance when dealing with large collections of data. These methods will themselves return an instance of Enumerable, so they can be chained together to perform multiple transformations at once, further improving performance. Examples of lazy-execution methods include Select, GroupBy, and Distinct.
(Note: as part of their iteration, some deferred-execution methods must first iterate over the entire underlying collection before they can begin their own iteration. Take caution when applying these methods to large collections as they can block the thread while the initial computation takes place. Examples of this are Reverse and OrderBy.)
To create an Enumerable out of a List
, Set
, or any other Iterable
,
use the Enumerable.from factory method or use the E
convenience global
function:
final list = [0, 1, 2, 3, 4];
// This works
final first = Enumerable.from(list);
// This also works
final second = E(list);
Enumerable extends Iterable
, so any instance of Enumerable can be
iterated over using a traditional for loop:
final list = [0, 1, 2, 3, 4];
for (var element in E(list).Select((i) => i * 2)) {
print(element);
}
// Output:
// 0
// 2
// 4
// 6
// 8
Enumerable objects can be converted back into Dart collections by utilizing the Enumerable.ToList, Enumerable.ToMap, or Enumerable.ToSet methods.
final list = [0, 1, 2, 3, 4];
final result = E(list).Select((i) => i * 2).ToList();
print(result);
// Output:
// [0, 2, 4, 6, 8]
- Inheritance
- Object
- Iterable<
T> - Enumerable
Constructors
- Enumerable()
- Enumerable.empty()
-
Creates an empty enumerable. [...]
factory
-
Enumerable.from(Iterable<
T> iterable) -
factory
- Enumerable.repeat(T value, int count)
-
Creates an enumerable [...]
factory
Properties
- first → T
-
Returns the first element. [...]
read-only, inherited
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- isEmpty → bool
-
Returns
true
if there are no elements in this collection. [...]read-only, inherited - isNotEmpty → bool
-
Returns true if there is at least one element in this collection. [...]
read-only, inherited
-
iterator
→ Iterator<
T> -
Returns a new
Iterator
that allows iterating the elements of thisIterable
. [...]read-only, inherited - last → T
-
Returns the last element. [...]
read-only, inherited
- length → int
-
Returns the number of elements in this. [...]
read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
- single → T
-
Checks that this iterable has only one element, and returns that element. [...]
read-only, inherited
Methods
-
Aggregate(
[Aggregator< T> aggregator, T initialValue ]) → T - Aggregates the enumerable into a single value. [...]
-
All(
[Condition< T> condition ]) → bool -
Returns
true
if all elements match a condition andfalse
otherwise. [...] -
Any(
[Condition< T> condition ]) → bool -
Returns
true
if any one element matches a condition andfalse
otherwise. [...] -
Append(
T newElement) → Enumerable< T> - Inserts an element to the end of the enumerable. [...]
-
Average(
) → T - Calculates the average of all numerical values in the enumerable. [...]
-
Cast<
TResult> ([CastTransformer< T, TResult> transformer ]) → Enumerable<TResult> - Casts each element in the enumerable from one type to another. [...]
-
Concat(
Iterable< T> other) → Enumerable<T> - Concatenates this enumerable and another collection. [...]
-
Contains(
T value, { EqualityComparer< T> comparer }) → bool -
Returns
true
if the specified element exists in the enumerable andfalse
otherwise. [...] -
Count(
) → int - Returns the number of elements in the enumerable. [...]
-
CountIf(
Condition< T> condition) → int - Returns the number of elements in the enumerable that match a condition. [...]
-
DefaultIfEmpty(
T value) → Enumerable< T> -
Returns either this enumerable or a new enumerable containing
value
if this enumerable is empty. [...] -
Distinct(
{EqualityComparer< T> comparer }) → Enumerable<T> - Returns an enumerable representing the distinct values of this enumerable. [...]
-
ElementAt(
int index) → T - Returns the element at the specified index. [...]
-
ElementAtOrDefault(
int index, { T defaultValue }) → T - Returns the element at the specified index or a default value of one is not found. [...]
-
Except(
Iterable< T> other, { EqualityComparer<T> comparer }) → Enumerable<T> - Returns the set difference between the enumerable and the given collection. [...]
-
First(
{Condition< T> condition }) → T - Returns the first element in the enumerable, optionally matching a specified condition. [...]
-
FirstOrDefault(
{Condition< T> condition, T defaultValue }) → T - Returns the first element in the enumerable or a default value if there is none, optionally matching a specified condition. [...]
-
GroupBy<
TKey> (Selector< T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<IGrouping< TKey, T> > - Groups the elements in the enumerable by a key. [...]
-
GroupByValue<
TKey, TValue> (Selector< T, TKey> keySelector, Selector<T, TValue> valueSelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<IGrouping< TKey, TValue> > - Groups the elements in the enumerable by a key and maps the elements to a new value. [...]
-
GroupJoin<
TInner, TKey, TResult> (Iterable< TInner> inner, Selector<T, TKey> outerKeySelector, Selector<TInner, TKey> innerKeySelector, GroupSelector<T, Iterable< resultSelector, { EqualityComparer<TInner> , TResult>TKey> keyComparer }) → Enumerable<TResult> -
Joins elements in the enumerable with a group of all elements in the
inner
collection that match the generated key. [...] -
GroupSelect<
TKey, TResult> (Selector< T, TKey> keySelector, GroupSelector<TKey, Iterable< resultSelector, { EqualityComparer<T> , TResult>TKey> keyComparer }) → Enumerable<TResult> - Groups the elements in the enumerable by a key and maps the groups to a new element. [...]
-
GroupSelectValue<
TKey, TValue, TResult> (Selector< T, TKey> keySelector, Selector<T, TValue> valueSelector, GroupSelector<TKey, Iterable< resultSelector, { EqualityComparer<TValue> , TResult>TKey> keyComparer }) → Enumerable<TResult> - Groups the elements in the enumerable by a key, maps the elements to a new value, and maps the groups to a new element. [...]
-
Intersect(
Iterable< T> other, { EqualityComparer<T> comparer }) → Enumerable<T> - Returns the set intersection between the enumerable and the given collection. [...]
-
Join<
TSecond, TKey, TResult> (Iterable< TSecond> other, Selector<T, TKey> outerKeySelector, Selector<TSecond, TKey> innerKeySelector, ZipSelector<T, TSecond, TResult> selector) → Enumerable<TResult> -
Finds keys in this enumerable with matching keys in the
other
collection and returns a value that is the result of the corresponding elements being merged. [...] -
Last(
{Condition< T> condition }) → T - Returns the last element in the enumerable, optionally matching a specified condition. [...]
-
LastOrDefault(
{Condition< T> condition, T defaultValue }) → T - Returns the last element in the enumerable, optionally matching a specified condition. [...]
-
Max(
[EqualityComparer< T> comparer ]) → T - Returns the maximum value in the enumerable. [...]
-
Min(
[EqualityComparer< T> comparer ]) → T - Returns the minimum value in the enumerable. [...]
-
OfType<
TResult> () → Enumerable< TResult> - Returns all elements in the enumerable that are castable to the specified type. [...]
-
OrderBy<
TKey> (Selector< T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<T> - Sorts the enumeration in ascending (least-to-greatest) order. [...]
-
OrderByDescending<
TKey> (Selector< T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<T> - Sorts the enumeration in descending (greatest-to-least) order. [...]
-
Prepend(
T newElement) → Enumerable< T> - Inserts an element at the beginning of the enumerable. [...]
-
Reverse(
) → Enumerable< T> - Reverses the order of the enumerable. [...]
-
Select<
TResult> (Selector< T, TResult> selector) → Enumerable<TResult> - Applies a mapping function to the elements in the enumerable. [...]
-
SelectMany<
TResult> (ManySelector< T, TResult> selector) → Enumerable<TResult> - Maps elements in an enumerable to collections and then flattens those collections into a single enumerable. [...]
-
SequenceEqual(
Iterable< T> other, { EqualityComparer<T> comparer }) → bool -
Returns
true
if this enumerable is equivalent to the given collection. [...] -
Single(
[Condition< T> condition ]) → T -
Returns the single element in the element, optionally matching a
condition
. [...] -
SingleOrDefault(
T defaultValue, [ Condition< T> condition ]) → T -
Returns the single element in the element, optionally matching a
condition
, or adefaultValue
if no such element exists. [...] -
Skip(
int count) → Enumerable< T> -
Skips the first
count
elements in the enumerable. [...] -
SkipWhile(
Condition< T> condition) → Enumerable<T> -
Skips all the elements at the beginning of the enumerable that match the
condition
. [...] -
Sum<
TResult extends num> ([Selector< T, TResult> selector ]) → TResult -
Calculates the sum of the elements in an enumerable, optionally using
selector
to obtain the value to be summed. [...] -
Take(
int count) → Enumerable< T> -
Takes the first
count
elements in the enumerable and discards the rest. [...] -
TakeWhile(
Condition< T> condition) → Enumerable<T> -
Takes all the elements at the beginning of the enumerable that match the
condition
and discards the rest. [...] -
ThenBy<
TKey> (Selector< T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<T> - Adds a secondary sorting pass to enumeration in ascending (least-to-greatest) order. [...]
-
ThenByDescending<
TKey> (Selector< T, TKey> keySelector, { EqualityComparer<TKey> keyComparer }) → Enumerable<T> - Adds a secondary sorting pass to enumeration in descending (greatest-to-least) order. [...]
-
ToList(
{bool growable: false }) → List< T> -
Converts the enumerable to a Dart
List
. [...] -
ToMap<
TKey, TValue> (Selector< T, TKey> keySelector, Selector<T, TValue> valueSelector) → Map<TKey, TValue> -
Converts the enumerable to a Dart
Map
. [...] -
ToSet(
) → Set< T> -
Converts the enumerable to a Dart
Set
. [...] -
Union(
Iterable< T> other, { EqualityComparer<T> comparer }) → Enumerable<T> - Returns the set union between the enumerable and the given collection. [...]
-
Where(
Condition< T> condition) → Enumerable<T> -
Filters the enumerable to elements that match the
condition
. [...] -
Zip<
TSecond, TResult> (Iterable< TSecond> other, ZipSelector<T, TSecond, TResult> selector) → Enumerable<TResult> - Combines the values of the enumerable and another collection into an enumerable of new values. [...]
-
any(
bool test(E element)) → bool -
Checks whether any element of this iterable satisfies
test
. [...]inherited -
cast<
R> () → Iterable< R> -
Provides a view of this iterable as an iterable of
R
instances. [...]inherited -
contains(
Object element) → bool -
Returns true if the collection contains an element equal to
element
. [...]inherited -
elementAt(
int index) → T -
Returns the
index
th element. [...]inherited -
every(
bool test(E element)) → bool -
Checks whether every element of this iterable satisfies
test
. [...]inherited -
expand<
T> (Iterable< T> f(E element)) → Iterable<T> -
Expands each element of this
Iterable
into zero or more elements. [...]inherited -
firstWhere(
bool test(E element), { T orElse() }) → T -
Returns the first element that satisfies the given predicate
test
. [...]inherited -
fold<
T> (T initialValue, T combine(T previousValue, E element)) → T -
Reduces a collection to a single value by iteratively combining each
element of the collection with an existing value [...]
inherited
-
followedBy(
Iterable< T> other) → Iterable<T> -
Returns the lazy concatentation of this iterable and
other
. [...]inherited -
forEach(
void f(E element)) → void -
Applies the function
f
to each element of this collection in iteration order.inherited -
join(
[String separator = "" ]) → String -
Converts each element to a
String
and concatenates the strings. [...]inherited -
lastWhere(
bool test(E element), { T orElse() }) → T -
Returns the last element that satisfies the given predicate
test
. [...]inherited -
map<
T> (T f(E e)) → Iterable< T> -
Returns a new lazy
Iterable
with elements that are created by callingf
on each element of thisIterable
in iteration order. [...]inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
reduce(
T combine(E value, E element)) → T -
Reduces a collection to a single value by iteratively combining elements
of the collection using the provided function. [...]
inherited
-
singleWhere(
bool test(E element), { T orElse() }) → T -
Returns the single element that satisfies
test
. [...]inherited -
skip(
int count) → Iterable< T> -
Returns an
Iterable
that provides all but the firstcount
elements. [...]inherited -
skipWhile(
bool test(E value)) → Iterable< T> -
Returns an
Iterable
that skips leading elements whiletest
is satisfied. [...]inherited -
take(
int count) → Iterable< T> -
Returns a lazy iterable of the
count
first elements of this iterable. [...]inherited -
takeWhile(
bool test(E value)) → Iterable< T> -
Returns a lazy iterable of the leading elements satisfying
test
. [...]inherited -
toList(
{bool growable: true }) → List< T> -
Creates a
List
containing the elements of thisIterable
. [...]inherited -
toSet(
) → Set< T> -
Creates a
Set
containing the same elements as this iterable. [...]inherited -
toString(
) → String -
Returns a string representation of (some of) the elements of
this
. [...]inherited -
where(
bool test(E element)) → Iterable< T> -
Returns a new lazy
Iterable
with all elements that satisfy the predicatetest
. [...]inherited -
whereType<
T> () → Iterable< T> -
Returns a new lazy
Iterable
with all elements that have typeT
. [...]inherited
Operators
-
operator ==(
dynamic other) → bool -
The equality operator. [...]
inherited
Static Methods
-
range(
int start, int count, { int increment: 1 }) → Enumerable< int> - Creates an enumerable containing values composed of the specified range. [...]