Junior — Middle
Can you explain what is meant by the data structure Map and how it works?
sobes.tech AI
Answer from AI
Map is a data structure that stores key-value pairs, allowing quick access to values by key. In Dart (and Flutter), Map is implemented as a hash table, providing efficient access, insertion, and deletion of elements.
For example, a Map can be used to store user settings, where the key is the setting name and the value is its value.
Example of using Map in Dart:
void main() {
Map<String, int> ages = {
'Ivan': 25,
'Maria': 30,
};
// Adding an element
ages['Peter'] = 40;
// Getting a value by key
print('Maria's age: ${ages['Maria']}');
// Checking if a key exists
if (ages.containsKey('Ivan')) {
print('Ivan is in the Map');
}
}