Dart by Example: Map

  1. main() {
  2. // adding keys
  3. var colors = new Map();
  4. colors['blue'] = false;
  5. colors['red'] = true;
  6. print(colors);
  7. // curly bracket literals can also be used:
  8. var shapes = {
  9. 'square': false,
  10. 'triangle': true
  11. };
  12. print(shapes);
  13. // keys and values can be iterated.
  14. // HashMap iterates in arbitrary order, while LinkedHashMap, and SplayTreeMap
  15. // iterate in the order they were inserted into the map.
  16. for (var key in shapes.keys) print(key);
  17. for (var value in shapes.values) print(value);
  18. }
  19.  
  1. $ dart map.dart
  2. {blue: false, red: true}
  3. {square: false, triangle: true}
  4. square
  5. triangle
  6. false
  7. true

by @jryanio | source | license