> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart Map

A `Map` in Dart is a collection of key-value pairs, formally defined by the generic interface `Map<K, V>`. It associates a unique key of type `K` with a value of type `V`. By default, Dart implements maps using `LinkedHashMap`, meaning the collection maintains the insertion order of its elements while providing constant-time `O(1)` complexity for lookups, insertions, and deletions.

## Initialization and Syntax

Maps can be instantiated using map literals or constructors. Dart's type inference automatically resolves `K` and `V` based on the provided literal values.

```dart theme={"dark"}
// Map literal with inferred type Map<String, int>
var inferredMap = {'alpha': 1, 'beta': 2};

// Explicit generic type declaration
Map<String, dynamic> explicitMap = {
  'id': 101,
  'isActive': true,
};

// Constructor initialization
var emptyMap = Map<int, String>();
```

## Access and Mutation

Values are accessed and mutated using the subscript operator `[]`. Because a requested key might not exist in the map, the subscript operator always returns a nullable type `V?`.

```dart theme={"dark"}
var config = {'retries': 3, 'timeout': 5000};

// Reading values
int? retries = config['retries']; // Returns 3
int? port = config['port'];       // Returns null

// Mutating values
config['port'] = 8080;            // Inserts a new key-value pair
config['retries'] = 5;            // Updates the value of an existing key
```

## Core Properties

The `Map` interface exposes several properties that return `Iterable` objects, allowing for functional transformations of the map's structural components.

* **`keys`**: Returns an `Iterable<K>` containing all keys.
* **`values`**: Returns an `Iterable<V>` containing all values.
* **`entries`**: Returns an `Iterable<MapEntry<K, V>>`, representing both keys and values as single objects.
* **`length`**: Returns the integer count of key-value pairs.
* **`isEmpty` / `isNotEmpty`**: Boolean properties evaluating the map's length.

## Essential Methods

Dart provides built-in methods for safe mutation, validation, and transformation of map data.

```dart theme={"dark"}
var data = {'a': 1, 'b': 2};

// Adds the key-value pair only if the key does not currently exist
data.putIfAbsent('c', () => 3);

// Updates the value for a key. Throws an error if the key doesn't exist 
// unless the optional ifAbsent callback is provided.
data.update('a', (value) => value + 10, ifAbsent: () => 10);

// Removes the key and its associated value, returning the removed value (V?)
int? removedValue = data.remove('b');

// Boolean checks for existence
bool hasKey = data.containsKey('a');     // true
bool hasValue = data.containsValue(99);  // false

// Clears all entries, resulting in an empty map
data.clear();
```

## Iteration

Maps can be traversed using the `forEach` method or via `for-in` loops operating on the `entries` iterable.

```dart theme={"dark"}
var metrics = {'cpu': 45, 'ram': 1024};

// Using the forEach method
metrics.forEach((key, value) {
  print('$key: $value');
});

// Using a for-in loop with MapEntry
for (MapEntry<String, int> entry in metrics.entries) {
  print('${entry.key}: ${entry.value}');
}
```

## Alternative Implementations

While `LinkedHashMap` is the default, the `dart:collection` library provides alternative implementations for specific algorithmic requirements:

* **`HashMap<K, V>`**: An unordered hash-table implementation. It does not guarantee iteration order but can offer marginal performance improvements over `LinkedHashMap` in highly volatile datasets.
* **`SplayTreeMap<K, V>`**: A self-balancing binary search tree implementation. It requires keys to be mutually comparable and maintains the map in a sorted order based on the keys.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
