I usually use Map
when I want to have key-value pairs because I can directly get the value by key. As a example if you have Map
of employes and if you keep the employee id as a key, you can easily access it. Specially for search, or for drop down this is elegant.
Most of the time if I can avoid Map
and I will use List
because it is very easy for me to handle large amount of data(In flutter I can throw a list to a ListView easily).
But both have there own greatness for depending on the scenario. And note that Map
cannot have duplicate key, where List
doesnt have like that limitation(Ex: Map
can use to avoid duplicate).
var locations = {
{
'country': 'Egypt',
'city': 'Cairo',
'Latitude': 30.033333,
'Longitude': 31.233334,
'utcOffset': 2
},
{
'country': 'Thailand',
'city': 'Bangkok',
'Latitude': 13.7563,
'Longitude': 100.5018,
'utcOffset': 7
},
};
for (var element in locations) {
print(element["country"]);
}
// or
locations.forEach((element) => print(element["country"]));
Here what doc contains about this:
Map<K, V>
class
A collection of key/value pairs, from which you retrieve a value using
its associated key.
There is a finite number of keys in the map, and each key has exactly
one value associated with it.
Maps, and their keys and values, can be iterated. The order of
iteration is defined by the individual type of map.
List<E>
class
An indexable collection of objects with a length.
Subclasses of this class implement different kinds of lists. The most
common kinds of lists are:
Fixed-length list. An error occurs when attempting to use operations that can change the length of the list.
Growable list. Full implementation of the API defined in this class.
The default growable list, as returned by new List()
or []
, keeps an
internal buffer, and grows that buffer when necessary. This guarantees
that a sequence of add operations will each execute in amortized
constant time. Setting the length directly may take time proportional
to the new length, and may change the internal capacity so that a
following add operation will need to immediately increase the buffer
capacity. Other list implementations may have different performance
behavior.