Maps and HashMaps

 void main() {

  /*var map_name = {
"key1" : "value1",
"key2" : 2,
"key3" : 3.0,
"key4" : true,
};
print(map_name);
print(map_name["key2"]);
print(map_name["key5"]); //"key5" does not exist.so the value will be null.

map_name["key1"] = "Raman"; //Here, the value will be overwritten
print(map_name["key1"]);

map_name["Key1"] = "Ajay"; /*Here,"Key1" : "Ajay" is added because "key1" & "Key1" both are different.
so new key will be added automatically.*/
print(map_name);*/

/*var map_name = { //Representing map using literal(First way).
"Name" : "value1",
"YearOfExperience" : 2,
"Avg.Rating" : 3.0,
"CanLocateToOffice" : true,
};*/

var mapName =
Map(); /*Representing map using constructor(Second way).Here, Map(); constructor underline indicates
for using literal -> "{}";here,creating object of class.This is the another way to
representing the map.Both ways perform same action.*/

mapName["Name"] = "Raman";
mapName["YearOfExperience"] = 2;
mapName["Avg.Rating"] = 3.0;
mapName["CanLocateToOffice"] = true;

print(mapName.isNotEmpty); //Different different operations performed on Map
print(mapName.isEmpty);
print(mapName.length);
print(mapName.keys);
print(mapName.values);
print(mapName.containsKey("Name"));
print(mapName.containsValue(false));
print(mapName
.remove("CanLocateToOffice")); //Removes "CanLocateToOffice" key from map
print(mapName);
}

/// OUTPUT:
/// true
/// false
/// 4
/// (Name, YearOfExperience, Avg.Rating, CanLocateToOffice)
/// (Raman, 2, 3.0, true)
/// true
/// false
/// true
/// {Name: Raman, YearOfExperience: 2, Avg.Rating: 3.0}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter