Introduction to Dart LinkedHashMap class:
The LinkedHashMap class of Dart is a hash-table-based Map implementation. The advantage of this class is that it remembers the insertion order of the keys and if we iterate over the map, it iterates in the same order they were inserted.
If we remove a key-value pair and add it again, it will be inserted at the end. Otherwise, the iteration order won’t be changed. Even if we change the value of an existing key, the iteration order will remain the same.
This post will list down all the constructors, methods and properties of a Dart LinkedHashMap.
Constructors of LinkedHashMap:
We can use the following constructors to create a LinkedHashMap object:
LinkedHashMap({bool equals(K, K)?, int hashCode(K)?, bool isValidKey(dynamic)?})
This constructor creates a LinkedHashMap. It takes three optional parameters:
- The first parameter equals defines the comparison function of the keys. It uses Object.== by default.
- The second parameter hashCode is used to create hash code for the keys. By default, Object.hashCode is used.
- The third parameter isValidKey is used to check if the key is valid or not.
LinkedHashMap.from(Map o)
This constructor creates a LinkedHashMap from another map o.
LinkedHashMap.fromEntries(Iterable<MapEntry<K, V>> e)
This constructor creates a LinkedHashMap from a given map-entries.
LinkedHashMap.fromIterable(Iterable it, {K key(dynamic element)?, V value(dynamic element)?})
Create one LinkedHashMap from an iterable, the keys and values are derived from the iterable.
LinkedHashMap.fromIterables(Iterable k, Iterable v)
It takes two parameters, the first one is iterable for the keys and the second one is iterable for the values.
LinkedHashMap.of(Map<K, V> o)
It creates a LinkedHashMap with the key-value pairs of the Map o.
LinkedHashMap.identity()
Create one identity-based LinkedHashMap.
Properties of LinkedHashMap:
All the properties of LinkedHashMap are inherited. The following are the available properties:
hashCode → int
It is the hash code for the LinkedHashMap object.
isEmpty → bool
This property returns one boolean value. It is true if the LinkedHashMap is empty, else false.
isNotEmpty → bool
It returns false for an empty LinkedHashMap, and true otherwise.
length → int
The length of the LinkedHashMap, i.e. the number of pairs it has.
keys → Iterable
It returns an iterable holding the keys of the LinkedHashMap.
values → Iterable
Iterable with the values.
entries → Iterable<MapEntry<K, V>>
The map entries of the current LinkedHashMap.
runtimeType → Type
Runtime type of the current object.
Methods of LinkedHashMap:
Following are the inherited methods available in a LinkedHashMap object:
forEach(void action(K k, V v)) → void
Applies the function action to each key-value pair of the object.
map<K2, V2>(MapEntry<K2, V2> convert(K k, V v)) → Map<K2, V2>
It uses the convert function to convert all entries of the current map and a new map is returned with the new values.
addAll(Map<K, V> o) → void
Adds all pairs of the other map o to this map.
addEntries(Iterable<MapEntry<K, V>> entries) → void
Add all pairs of entries to this map.
putIfAbsent(K k, V ifAbsent()) → V
Add a new entry with the pair if not available in the current object.
update(K key, V update(V value), {V ifAbsent()?}) → V
Update the value with the key in the current LinkedHashMap.
updateAll(V update(K key, V value)) → void
Update all the values defined by the update function.
remove(Object? k) → V?
Remove the key-value pair with the key k from the current LinkedHashMap.
removeWhere(bool test(K key, V value)) → void
Remove all the pairs that are satisfied by the given function test.
clear() → void
Remove all pairs from the current object.
containsKey(Object? key) → bool
Check if the LinkedHashMap contains the key or not.
containsValue(Object? value) → bool
Check if the LinkedHashMap contains the value or not.
cast<RK, RV>() → Map<RK, RV>
Cast the map.
noSuchMethod(Invocation invocation) → dynamic
This is invoked if the program is trying to access any invalid method or property.
toString() → String
Return one string representation of the current object.
Refer:
You might also like:
- How to add and remove items from a HashSet in Dart
- Union and intersection of two HashSet in Dart
- Different ways to create a HashSet in Dart
- 3 ways to find if a HashSet is empty or not in Dart
- Dart HashSet.take and HashSet.takeWhile methods
- Dart HashSet where and whereType explanation with examples
- Dart HashSet fold and reduce methods explanation with examples
- Dart HashSet skip and skipWhile methods explanation with example