CollectionsExtensions<T> extension
Properties
-
firstOrNull
→ T?
-
get the first element return null
no setter
-
halfLength
→ int
-
no setter
-
lastOrNull
→ T?
-
get the last element if the list is not empty or return null
no setter
Methods
-
all(bool predicate(T pred)?)
→ bool
-
Returns
true
if all elements match the given predicate.
Example:
5, 19, 2
.all(isEven), isFalse)
6, 12, 2
.all(isEven), isTrue)
-
associate(dynamic key(dynamic element), dynamic value(dynamic element))
→ Map
-
Creates a Map instance in which the keys and values are computed from the iterable.
-
chunks(int size)
→ Iterable<List<T>>
-
Splits the Iterable into chunks of the specified size
-
containsAll(Iterable<T> collection)
→ bool
-
Checks if all elements in the specified
collection
are contained in
this collection.
-
count([bool predicate(T element)?])
→ int
-
Return a number of the existing elements by a specific predicate
example:
final aboveTwenty =
User(33, "chicko"),
User(45, "ronit"),
User(19, "amsalam"),
.count((user) => user.age > 20); // 2
-
distinctBy(dynamic predicate(T selector))
→ List<T>
-
Returns a list containing only the elements from given collection having distinct keys.
-
drop(int n)
→ List<T>
-
Returns a list containing all elements except first
n
elements.
-
filter(bool test(T element))
→ List<T>
-
Returns a list containing only elements matching the given
predicate
.
-
filterNot(bool test(T element))
→ List<T>
-
Returns a list containing all elements not matching the given
predicate
and will filter nulls as well.
-
find(dynamic predicate(T selector))
→ T?
-
Returns the first element matching the given
predicate
, or null
if element was not found.
-
firstHalf()
→ List<T>
-
-
firstOrDefault(T defaultValue)
→ T
-
get the first element or provider default
example:
var name =
danny, ronny, james
.firstOrDefault"jack"
; // danny
var name = [].firstOrDefault"jack"
; // jack
-
firstOrNullWhere(bool predicate(T element))
→ T?
-
-
forEachIndexed(void action(T element, int index))
→ void
-
Performs the given action on each element on iterable, providing sequential index with the element.
item
the element on the current iteration
index
the index of the current iteration
-
getRandom()
→ T
-
-
groupBy<T, K>(K key(T e))
→ Map<K, List<T>>
-
Groups the elements in values by the value returned by key.
-
intersect(Iterable<T> other)
→ Set<T>
-
Returns a set containing all elements that are contained
by both this set and the specified collection.
-
lastOrDefault(T defaultValue)
→ T
-
-
mapList<E>(E f(T e))
→ List<E>
-
-
secondHalf()
→ List<T>
-
-
sortBy<TKey>(TKey keySelector(T), {required EqualityComparer<TKey> keyComparer})
→ Iterable<T>
-
Sorts elements in the array in-place according to natural sort order of the value returned by specified
selector
function.
-
sortedDescending()
→ List<T>
-
Returns a new list with all elements sorted according to descending
natural sort order.
-
subtract(Iterable<T> other)
→ dynamic
-
Returns a set containing all elements that are contained by this collection
and not contained by the specified collection.
The returned set preserves the element iteration order of the original collection.
-
swap(int i, int j)
→ List<T>
-
returns a list with two swapped items
i
first item
j
second item
-
takeOnly(int n)
→ List<T>
-
Returns a list containing first
n
elements.
-
toMutableSet()
→ Set<T>
-
Convert iterable to set
-
toStack()
→ StackX<T>
-
will convert iterable into a Stack data structure
example:
1,2,3,4
.toStack()
stack.pop()
stack.push(5)
-
whereIndexed(IndexedPredicate<T> predicate)
→ Iterable<T>
-
Will retrun new Iterable with all elements that satisfy the predicate
predicate
,