forked from smartherd/DartTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
34_maps_and_hashmap.dart
49 lines (37 loc) · 1.17 KB
/
34_maps_and_hashmap.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Objectives
// 1. Maps
// --> KEY has to be unique
// --> VALUE can be duplicate
void main() {
Map<String, int> numbers = {"Alex": 1, "Bongo": 2, "Peter": 3};
Map<String, String> fruits = Map(); // Method 2: Using Constructor
fruits["apple"] = "red";
fruits["banana"] = "yellow";
fruits["guava"] = "green";
print(fruits.containsValue("green"));
print(
fruits.containsKey("apple")); // returns true if the KEY is present in Map
fruits.update(
"apple", (value) => "green"); // Update the VALUE for the given KEY
fruits.remove("apple"); // removes KEY and it's VALUE and returns the VALUE
fruits.isEmpty; // returns true if the Map is empty
fruits.length; // returns number of elements in Map
// fruits.clear(); // Deletes all elements
print(fruits["apple"]);
print("\n");
for (String key in fruits.keys) {
// Print all keys
print(key);
}
print("\n");
for (String value in fruits.values) {
// Print all values
print(value);
}
Map<int, int> m = {};
m[1] = 2;
print(m[1]);
print("\n");
fruits.forEach(
(key, value) => print("key: $key and value: $value")); // Using Lambda
}