# Top 10 Map methods you should know

In this article we will look at the top 10 utility methods you should know about the `Map` type in Dart. This is a follow up to the earlier “[Top 10](https://dev.to/graphicbeacon/top-10-array-utility-methods-you-should-know-dart-version--3217)” I did on List/Array types, so let’s jump right into it.

## **1\. addAll()**

This allows you to merge all key/value pairs of the provided map to the current one.

```dart
var user = {
  "firstName": "Tom",
  "age": 25,
};

user.addAll({
  "lastName": "Smith",
  "age": 26,
});

print(user); 
// => {"firstName": "Tom", "age": 26, "lastName": "Smith"}
```

If a key already exists, its value will be replaced. This works similarly to `Object.assign` in JavaScript.

## **2\. containsKey()**

Checks whether the given key exists.

```dart
print(user.containsKey("age")); // => true
print(user.containsKey("accessToken")); // => false
```

## **3\. containsValue()**

Checks whether the given value exists.

```dart
print(user.containsValue("Smith")); // => true
print(user.containsValue(40)); // => false
```

## **4\. forEach()**

Runs the given function over each key/value pair.

```dart
user.forEach((key, value) => print('Key: $key, Value: $value')); 
// => "Key: firstName, Value: Tom" "Key: age, Value: 26" "Key: lastName, Value: Smith"
```

## **5\. putIfAbsent()**

Adds a key/value pair if non-existent. If key already exists, a value will be set if there isn’t one.

```dart
user.putIfAbsent("accessToken", () => "abf329jklr90rnlk2..."); 
// => {"firstName": "Tom", "age": 26, "lastName": "Smith", "accessToken": "abf329jklr90rnlk2..."}
```

## **6\. remove()**

Removes the provided key and its associated value.

```dart
user.remove("accessToken"); // => abf329jklr90rnlk2...
```

This will return the value that was removed.

## **7\. removeWhere()**

Removes the key/value pair if the given condition is true.

```dart
user.removeWhere((key, value) => key == "lastName");
```

## **8\. clear()**

Removes every key/value pair in the map.

```dart
user.clear();
print(user); // => {}
```

## **9\. update()**

Updates the value for the given key.

```dart
user["age"] = 25;
user.update("age", (dynamic val) => ++val); // => 26
```

This also returns the new value. To prevent an error being thrown should the key not exist, there’s a third parameter:

```dart
user.update("name", (dynamic val) => "Jim", ifAbsent: () => "Jane");
print(user); // => {"age": 26, "name": "Jane"};
```

In most cases you could update using array bracket notation:

```dart
user["name"] = "Mary";
```

## **10\. Map.from()**

This technically is a constructor, but still useful as a utility. It creates and returns a copy of the provided map:

```dart
var userCopy = Map.from(user);
```

## **Conclusion**

I hope this was insightful and if this is your first exposure to Dart, read my [first steps tutorial](https://dev.to/graphicbeacon/learn-dart-before-you-flutter-2997) to grasp the basics. The code snippets for this article are available on [DartPad](https://dartpad.dartlang.org/b738e69b9985a5c11a841c94de1a1261).

**Like, share and** [**follow me**](https://twitter.com/creativ_bracket) for more articles on Dart. Thanks so much.

## **Further reading**

1. [Map&lt;K,V&gt; class Documentation](https://api.dartlang.org/stable/2.1.1/dart-core/Map-class.html)
    
2. [**Free Dart screencasts on Egghead.io**](https://egghead.io/instructors/jermaine-oppong)
