ListMap<K, V> class

A ListMap is a mutable, fixed-sized, and ordered map.

Compared to a LinkedHashMap, a ListMap is also ordered and has a slightly worse performance. But a ListMap takes less memory, and has some List methods like sort and shuffle. Also, you can efficiently read its information by index, by using the entryAt, keyAt and valueAt methods.

The disadvantage, of course, is that ListMap has a fixed size, while a LinkedHashMap does not.

Implemented types
Implementers
Available Extensions

Constructors

ListMap.empty()
ListMap.fromEntries(Iterable<MapEntry<K, V>> entries, {bool sort = false, int compare(K a, K b)?})
Creates a ListMap from entries. If entries contains the same keys multiple times, the last occurrence overwrites the previous value.
factory
ListMap.fromIterables(Iterable<K> keys, Iterable<V> values, {bool sort = false, int compare(K a, K b)?})
Creates a ListMap from the provided keys and values. If a key is repeated, the last occurrence overwrites the previous value.
factory
ListMap.of(Map<K, V> map, {bool sort = false, int compare(K a, K b)?})
Create a ListMap from the map.
ListMap.unsafe(Map<K, V> _map, {bool sort = false, int compare(K a, K b)?})
Creates a ListMap backed by the provided map. No defensive copy will be made, so you have to make sure that the original map won't change after the ListMap is created, since this will render the ListMap in an invalid state.
ListMap.unsafeFrom({required Map<K, V> map, required List<K> list})
Creates a ListMap backed by the provided map and list. No defensive copies will be made, so you have to make sure that:

Properties

entries Iterable<MapEntry<K, V>>
The map entries of this.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
isEmpty bool
Whether there is no key/value pair in the map.
no setteroverride
isNotEmpty bool
Whether there is at least one key/value pair in the map.
no setteroverride
keys Iterable<K>
The keys of this.
no setteroverride
length int
The number of key/value pairs in the map.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
values Iterable<V>
The values of this.
no setteroverride

Methods

addAll(Map<K, V> other) → void
Adds all key/value pairs of other to this map.
override
addEntries(Iterable<MapEntry<K, V>> newEntries) → void
Adds all key/value pairs of newEntries to this map.
override
cast<RK, RV>() ListMap<RK, RV>
Provides a view of this map as having RK keys and RV instances, if necessary.
override
clear() → void
Removes all entries from the map.
override
containsKey(Object? key) bool
Whether this map contains the given key.
override
containsValue(Object? value) bool
Whether this map contains the given value.
override
entry(K key) MapEntry<K, V>
Return the key/value entry for the given key, or throws if key is not in the map.
entryAt(int index) MapEntry<K, V>
Returns the indexth entry. The index must be non-negative and less than length. Index zero represents the first entry.
entryOrNull(K key) MapEntry<K, V>?
Return the key/value entry for the given key, or null if key is not in the map.
entryOrNullValue(K key) MapEntry<K, V?>
Return the key/value entry for the given key. If the key is not in the map, return MapEntry(key, null).
forEach(void f(K key, V value)) → void
Applies action to each key/value pair of the map.
override
get(covariant K key) → V?
The value for the given key, or null if key is not in the map.
getOrThrow(K key) → V
Returns the value if it exists, otherwise throws a StateError.
indexOfKey(K key, [int start = 0]) int
The first index of key in this list.
keyAt(int index) → K
Returns the indexth key. The index must be non-negative and less than length. Index zero represents the first key.
map<K2, V2>(MapEntry<K2, V2> f(K key, V value)) Map<K2, V2>
Returns a new map where all entries of this map are transformed by the given convert function.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
putIfAbsent(K key, V ifAbsent()) → V
Look up the value of key, or add a new entry if it isn't there.
override
remove(Object? key) → V
Removes key and its associated value, if present, from the map.
override
removeWhere(bool predicate(K key, V value)) → void
Removes all entries of this map that satisfy the given test.
override
shuffle([Random? random]) → void
Shuffles the keys of this map randomly.
sort([int compare(K a, K b)?]) → void
Sorts the keys of this map.
toString() String
A string representation of this object.
inherited
update(K key, V update(V value), {V ifAbsent()?}) → V
Updates the value for the provided key.
override
updateAll(V update(K key, V value)) → void
Updates all values.
override
valueAt(int index) → V
Returns the indexth value. The index must be non-negative and less than length. Index zero represents the first value.

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](covariant K key) → V?
The value for the given key, or null if key is not in the map.
override
operator []=(K key, V value) → void
Replaces the value of a key that already exists in the map. However, if the key is not already present, this will throw an error.
override

Static Methods

unsafeView<K, V>(Map<K, V> map) ListMap<K, V>
Creates a ListMap form the given map. If the map is already of type ListMap, return the same instance. This is unsafe because a ListMapView is fixed size, but the given map may not.