IndexedMap<T extends MapIndexable<I>, I> class

A hybrid container that provides O(1) lookup by id and ordered/indexed access.

T must expose a stable indexId of type I.

Internally maintains three synchronized structures:

  • A Map for O(1) lookup by id
  • A List for O(1) indexed access
  • A reverse index Map for O(1) id-to-index resolution
Mixed-in types

Constructors

IndexedMap({@Deprecated('This parameter is ignored and will be removed in 2.0.0.') Map<I, ItemWrapper<T, I>>? map, @Deprecated('This parameter is ignored and will be removed in 2.0.0.') List<ItemWrapper<T, I>>? list, DuplicatePolicy duplicatePolicy = DuplicatePolicy.replaceKeepPosition})
IndexedMap.fromIterable(Iterable<T> items, {DuplicatePolicy duplicatePolicy = DuplicatePolicy.replaceKeepPosition})
Build from an iterable. Duplicates are resolved via duplicatePolicy.
factory

Properties

asMapView → Map<I, ItemWrapper<T, I>>
Unmodifiable view of the backing map if needed.
no setter
duplicatePolicy → DuplicatePolicy
Controls behavior when adding an item with an existing id.
final
first → T
O(1) first item.
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
isEmpty → bool
Whether this collection has no elements.
no setteroverride
isNotEmpty → bool
Whether this collection has at least one element.
no setteroverride
iterator → Iterator<T>
Iterate items in order.
no setteroverride
keys → Iterable<I>
Return an iterable of all ids in order.
no setter
last → T
O(1) last item.
no setteroverride
length → int
Number of items.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
single → T
Checks that this iterable has only one element, and returns that element.
no setterinherited
values → Iterable<T>
Return read-only iterable of items in order.
no setter
wrappers → List<ItemWrapper<T, I>>
Unmodifiable view of wrappers if you need metadata externally.
no setter

Methods

add(T item) → bool
Append to the end. Handles duplicates per duplicatePolicy. Returns true if a new id was inserted or an existing id was replaced.
addAll(Iterable<T> items) → int
Add all items from items. Returns the number of items actually inserted or replaced (not ignored).
any(bool test(T item)) → bool
Checks whether any element of this iterable satisfies test.
inherited
cast<R>() → Iterable<R>
A view of this iterable as an iterable of R instances.
inherited
clear() → void
Clear everything.
contains(Object? element) → bool
O(1) check whether element is in this collection.
override
containsId(I id) → bool
True if the id exists.
elementAt(int index) → T
Returns the indexth element.
inherited
every(bool test(T item)) → bool
Checks whether every element of this iterable satisfies test.
inherited
expand<T>(Iterable<T> toElements(T element)) → Iterable<T>
Expands each element of this Iterable into zero or more elements.
inherited
firstWhere(bool test(T item), {T orElse()?}) → T
The first element that satisfies the given predicate test.
inherited
fold<T>(T initialValue, T combine(T previousValue, T 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>
Creates the lazy concatenation of this iterable and other.
inherited
forEach(void action(T element)) → void
Invokes action on each element of this iterable in iteration order.
inherited
getById(I id) → T?
O(1) lookup by id. Returns null if not present.
getWrapperById(I id) → ItemWrapper<T, I>?
O(1) wrapper lookup by id (if you need metadata).
indexOfId(I id) → int
O(1) index of an id, or -1 if missing.
insertAt(int index, T item) → void
Insert at a specific position. If id exists, behavior depends on duplicatePolicy.
join([String separator = ""]) → String
Converts each element to a String and concatenates the strings.
inherited
lastWhere(bool test(T item), {T orElse()?}) → T
The last element that satisfies the given predicate test.
inherited
map<T>(T toElement(T e)) → Iterable<T>
The current elements of this iterable modified by toElement.
inherited
moveIdTo(I id, int toIndex) → bool
Move an item identified by id to a new toIndex. Returns true if moved, false if id not found, toIndex out of bounds, or item is already at toIndex.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reduce(T combine(T value, T element)) → T
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
inherited
removeAt(int index) → T
Remove at index. Returns removed item.
removeById(I id) → T?
Remove by id. Returns removed item or null if not present.
removeWhere(bool test(T item)) → int
Remove all items matching test. Returns the number of items removed.
singleWhere(bool test(T item), {T orElse()?}) → T
The single element that satisfies test.
inherited
skip(int count) → Iterable<T>
Creates an Iterable that provides all but the first count elements.
inherited
skipWhile(bool test(T item)) → Iterable<T>
Creates an Iterable that skips leading elements while test is satisfied.
inherited
sortBy(Comparator<T> comparator) → void
Re-sort the list in place using the provided comparator over T. Map remains valid since ids don't change.
take(int count) → Iterable<T>
Creates a lazy iterable of the count first elements of this iterable.
inherited
takeWhile(bool test(T item)) → Iterable<T>
Creates a lazy iterable of the leading elements satisfying test.
inherited
toList({bool growable = false}) → List<T>
Return a defensive copy of the ordered items.
override
toMap() → Map<I, T>
Return a Map from id to item.
toSet() → Set<T>
Creates a Set containing the same elements as this iterable.
inherited
toString() → String
A string representation of this object.
override
upsertKeepingPosition({required I oldId, required T newItem}) → bool
Update an item whose id may have changed.
where(bool test(T item)) → Iterable<T>
Creates a new lazy Iterable with all elements that satisfy the predicate test.
inherited
whereType<T>() → Iterable<R>
Creates a new lazy Iterable with all elements that have type T.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited
operator [](int index) → T
O(1) by index (bounds-checked by List).
operator []=(int index, T newItem) → void
Replace the item at a position. Id consistency is enforced.