Dart: Should I prefer to iterate over Map.entries or Map.values?

We should iterate map based on the requirement.

  1. If we required to iterate only keys

    map.keys.forEach((k) => print("Key : $k"));

  2. If we required to iterate only values

    map.values.forEach((v) => print("Value: $v"));

  3. If we required to iterate both key values.

    map.forEach((k, v) => print("Key : $k, Value : $v"));


If you iterate over entries a MapEntry instance is created for every key-value combination.

If you only need values, iterating map.values is more efficient.

Tags:

Dart