countItems<K, V> static method
Total number of elements across every iterable value in inputMap.
Folds the .length of each value into a single running int, so the
result is the combined element count of all the iterables, ignoring the
keys entirely. Generic over key type K and element type V, so it
works on any Map<K, List<V>>, Map<K, Set<V>>, or
Map<K, Iterable<V>>. Typical use is sizing grouped collections such as
Map<Section, List<Item>> (e.g. counting selected items across grouped
sections).
Edge cases:
- Empty map or all-empty values: returns
0. Setvalues: counts the post-deduplication.length, not the number of values that were inserted ({1, 1}contributes1, not2).- Lazy iterables (from
Iterable.generate,map(...),where(...)):.lengthmaterializes the iterable, so the count is correct. - Element CONTENT is irrelevant — only
.lengthis read. A multi-code- unit emoji, a combining-mark string, andnulleach count as exactly one element. - Order-independent: rearranging keys or empty/non-empty values does not change the result, because addition is commutative.
- Non-mutating: neither
inputMapnor any of its iterable values is altered;foldonly reads.length.
Example:
MapExtensions.countItems(<String, List<int>>{
'a': <int>[1, 2],
'b': <int>[3],
'c': <int>[],
}); // 3
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
static int countItems<K, V>(Map<K, Iterable<V>> inputMap) =>
inputMap.values.fold<int>(0, (int sum, Iterable<V> iter) => sum + iter.length);