Introduction to HashMap<K,V> class in Dart:
The HashMap class can be used to store key-value pairs. We can use the keys to access and modify its values.
The keys of a HashMap can be any object but they should implement the Object.== and Object.hashCode methods.
The HashMap is an unordered implementation. So the iteration doesn’t follow any order. If the map is modified, the iteration order changes.
Constructors of HashMap:
We can create a HashMap in different ways. Following are the constructors available:
HashMap({bool equals(K, K)?, int hashCode(K)?, bool isValidKey(dynamic)?})
This constructor creates a HashMap.
HashMap.from(Map o)
Create one HashMap from another Map o with the same key-value pairs.
HashMap.fromEntries(Iterable<MapEntry<K, V>> e)
Create one HashMap from the given iterable entries.
HashMap.fromIterable(Iterable iterable, {K key(dynamic element)?, V value(dynamic element)?})
Creates one HashMap from the Iterable.
HashMap.fromIterables(Iterable k, Iterable v)
Creates a HashMap associating the keys k to values v.
HashMap.identity()
Creates an unordered identity-based map.
HashMap.of(Map<K, V> o)
Create one HashMap containing the key-value pairs of the Map o.
Properties of HashMap:
Following properties are available in a dart HashMap:
entries → Iterable<MapEntry<K, V>>
It is the entries of this HashMap.
hashCode → int
It holds the hash code.
isEmpty → bool
It is a boolean value, true if the HashMap is empty, false otherwise.
isNotEmpty → bool
It is the opposite of isEmpty.
keys → Iterable
It is the keys of the HashMap.
length → int
Length of the HashMap.
values → Iterable
The values of the HashMap.
runtimeType → Type
Runtime type of the HashMap.
Methods of HashMap:
Following methods are available for a HashMap:
containsKey(Object? k) → bool
Checks if the key k is in the HashMap or not.
addAll(Map<K, V> m) → void
This method adds all the key-value pairs of another map m to the current HashMap.
containsValue(Object? v) → bool
Check if the value v is in the HashMap or not.
addEntries(Iterable<MapEntry<K, V>> e) → void
Add the key-value pairs of the entries e to the HashMap.
forEach(void f(K k, V v)) → void
Use the function f on each item of the HashMap.
cast<RK, RV>() → Map<RK, RV>
Get a view of this map.
map<K2, V2>(MapEntry<K2, V2> convert(K k, V v)) → Map<K2, V2>
It converts the current HashMap to a new Map by using the given convert function.
update(K k, V update(V v), {V ifAbsent()?}) → V
It updates the value v for the key k.
clear() → void
It removes all items from the HashMap.
noSuchMethod(Invocation i) → dynamic
This method is invoked when we try to access any invalid method or property of the HashMap.
updateAll(V update(K k, V v)) → void
This method updates all values.
putIfAbsent(K k, V ifAbsent()) → V
It checks for the key k in the HashMap and adds it if not found.
remove(Object? k) → V?
It removes the pair with key k from the HashMap if found in the HashMap.
removeWhere(bool f(K k, V v)) → void
It removes all pairs from a map for those the function f returns true.
toString() → String
Get the string representation.
Reference:
You might also like:
- Get the only single element that satisfy a condition in dart list
- Dart program to get the first n elements from a list
- How to create a string from ASCII values in Dart
- Dart StringBuffer class explanation with examples
- 5 ways in Dart to print the multiplication table
- Introduction to Queue in Dart and its methods
- How to add and remove items from a Queue in Dart
- How to remove and retain items from Queue in Dart with condition
- Dart Queue reduce function example