Dart:
Using associative arrays

How to:

Dart offers a straightforward syntax for creating and manipulating Maps. Below are examples demonstrating basic operations like creation, adding elements, and fetching values.

void main() {
  // Creating a map
  var fruitColors = {
    'apple': 'red',
    'banana': 'yellow',
    'grape': 'purple'
  };

  // Adding a new key-value pair
  fruitColors['orange'] = 'orange';

  // Accessing a value by its key
  print(fruitColors['apple']); // Output: red

  // Updating a value
  fruitColors['banana'] = 'green';

  // Iterating over the Map
  fruitColors.forEach((fruit, color) {
    print('$fruit: $color');
  });
  // Sample Output:
  // apple: red
  // banana: green
  // grape: purple
  // orange: orange
}

For complex data structures or extended functionality, Dart programmers often rely on additional libraries. One such library is collection which provides advanced collection types and utilities. Although collection does not modify the basic way Maps are handled, it enriches them with utility functions and more sophisticated collection types. Here’s how you might use it for a more specific task, such as sorting a Map by its values:

First, ensure the collection package is included in your pubspec.yaml file:

dependencies:
  collection: ^1.15.0

Then, you can use it as follows:

import 'package:collection/collection.dart';

void main() {
  var fruitColors = {
    'apple': 'red',
    'banana': 'yellow',
    'grape': 'purple',
    'orange': 'orange'
  };

  // Sorting the Map by its values (colors)
  var sortedFruitsByColor = SplayTreeMap.from(
    fruitColors,
    (key1, key2) => fruitColors[key1]!.compareTo(fruitColors[key2]!)
  );

  print(sortedFruitsByColor);
  // Output:
  // {orange: orange, apple: red, banana: yellow, grape: purple}
}

This example demonstrates sorting a Map’s entries based on their values, showcasing how Dart and its vibrant ecosystem can nimbly handle associative arrays for more sophisticated data manipulation.