-
Notifications
You must be signed in to change notification settings - Fork 387
/
33_maps_and_hashmap.dart
46 lines (32 loc) · 1.25 KB
/
33_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
// Objectives
// 1. Maps
// --> KEY has to be unique
// --> VALUE can be duplicate
void main() {
Map<String, int> countryDialingCode = { // Method 1: Using Literal
"USA": 1,
"INDIA": 91,
"PAKISTAN": 92
};
Map<String, String> fruits = Map(); // Method 2: Using Constructor
fruits["apple"] = "red";
fruits["banana"] = "yellow";
fruits["guava"] = "green";
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);
}
print("\n");
fruits.forEach((key, value) => print("key: $key and value: $value")); // Using Lambda
}