Skip to content

Commit c7e760c

Browse files
committed
Add Dart Map .addEntries() term entry
1 parent 4844888 commit c7e760c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
Title: '.addEntries()'
3+
Description: 'Adds multiple key-value pairs to an existing Map using an iterable of MapEntry objects.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Dart'
9+
- 'Map'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-dart'
13+
- 'paths/computer-science'
14+
---
15+
16+
In Dart, the **`.addEntries()`** method allows you to add multiple key-value pairs to an existing `Map` by providing it with an iterable of `MapEntry` objects. This is useful when inserting several entries at once, especially when transforming lists into maps or combining data from multiple sources.
17+
18+
## Syntax
19+
20+
```pseudo
21+
mapVariable.addEntries(iterableOfMapEntries)
22+
```
23+
24+
- `mapVariable`: The map you want to add entries into.
25+
- `iterableOfMapEntries`: An iterable containing `MapEntry` objects (`MapEntry(key, value)`).
26+
27+
## Example
28+
29+
The following example demonstrates how `.addEntries()` can be used to add multiple entries to a `Map`:
30+
31+
```dart
32+
void main() {
33+
Map<String, int> inventory = {
34+
'Apples': 5,
35+
'Bananas': 3
36+
};
37+
38+
// Creating an iterable of MapEntry objects
39+
var newItems = [
40+
MapEntry('Oranges', 4),
41+
MapEntry('Grapes', 10)
42+
];
43+
44+
inventory.addEntries(newItems);
45+
46+
print(inventory);
47+
}
48+
```
49+
50+
**Output:**
51+
52+
```shell
53+
{Apples: 5, Bananas: 3, Oranges: 4, Grapes: 10}
54+
```

0 commit comments

Comments
 (0)