Map_(Collection)_Dart

 import 'dart:ffi';


///Introduction to the Dart Map type
/// Map type allows you to manage a collection of key/value pairs. The Map type is
/// similar to the Dictionary type in other programming languages.
/// In a map, keys are unique. Each key has an associated value. Unlike keys, values can
/// be duplicated. Generally, you cannot add or remove keys while performing an
/// operation on the map

///CREATING A MAP
void main()
{
var fruits = {
"apple" : 1,
"banana" : 2,
"orange" : 3,
"pineapple" : 4,
"pomegranate" : 5,
"coconut" : 6,
"mango" : 7,
"grapes" : 8,
"papaya" : 9,
"watermelon" : 10,
"kiwi" : 11,
"Lemon" : 12,
"cherry" : 13,
"strawberry" : 14,
"blueberry" : 15,
"pear" : 16
};
// print(fruits);
// print("\n");

///ACCESSING ELEMENTS FROM A MAP
// print(fruits["grapes"]); //Printing "Value" of particular keys
// print(fruits["mango"]); //Printing "Value" of particular keys
// print(fruits["coconut"]); //Printing "Value" of particular keys
// print(fruits["watermelon"]); //Printing "Value" of particular keys

///ADDING ELEMENTS TO A MAP
// fruits["grapefruits"] = 17;
// // print(fruits);
//
// fruits["fig"] = 18;
//
// fruits["Apricot"] = 19;
//
// fruits["plum"] = 20;
// print(fruits);
// print("\n");

///UPDATING ELEMENTS TO A MAP
// fruits["apple"] = 0;
// print(fruits);
//
// fruits["banana"] = 22;
// print(fruits);
// print("\n");

///REMOVING ELEMENTS
// fruits.remove("plum");
// fruits.remove("Apricot");
// fruits.remove("fig");
// print(fruits);

/// The Map type provides some useful properties:
/// ● map.isEmpty – return true if the map has no element.
/// ● map.isNotEmpty – return true if the map has at least one element.
/// ● map.length – return the number of elements of the map.
/// ● map.keys – return a list of keys.
/// ● maps.values – return a list of values.

// print(fruits.isEmpty);
// print(fruits.isNotEmpty);
// print("Number of elements in the List are : ${fruits.length}");
// print(fruits.keys);
// print(fruits.values);

///Checking for the existence of the keys OR values
// print(fruits.containsKey("strawberry")); //true
// print(fruits.containsKey("All fruits")); //false

///Iterating over elements of a map
// var fruits1 = {
// "apple" : 10,
// "banana" : 20,
// "pineapple" : 30,
// "strawberry" : 40,
// "watermelon" : 50
// };

///for loop
// for(var name in fruits1.keys)
// {
// // print("$name : ${fruits1[name]}\n");
// // print("${fruits1.keys} : ${fruits1.values}");
// // print("${fruits1.keys}");
// // print("${fruits1.values}");
// }

///for loop
// for(var name1 in fruits1.entries)
// {
// print("${fruits1.keys} : ${fruits1.values}");
// }

///forEach loop
// fruits1.forEach((key, value) {
// return print("$key : $value");
// });

///PRACTICE
// var cars = {
// "Maruti" : 1,
// "Hyundai" : 2,
// "Toyota" : 3,
// "BMW" : 4,
// "Ford" : 5,
// "TATA" : 6,
// "Jeep" : 7,
// "Skoda" : 8,
// "Renault" : 9,
// "Mercedes" : 10
// };
// print(cars);

var fruitsDemo = {
"apple" : 1,
"banana" : 2,
"cherry" : 3,
4 : "pomegranate",
5 : "mango",
6 : "coconut",
};
print(fruitsDemo);
print("\n");
// print(fruitsDemo["banana"]);
// print(fruitsDemo["cherry"]);
// print(fruitsDemo[5]);
// print(fruitsDemo[6]);
//
// fruitsDemo["strawberry"] = 4;
// fruitsDemo[7] = "pineapple";
// print(fruitsDemo);

// fruitsDemo["banana"] = 7;
// print(fruitsDemo);

// fruitsDemo.remove(6);
// fruitsDemo.remove("cherry");
// print(fruitsDemo);

// print(fruitsDemo.isEmpty);
// print(fruitsDemo.isNotEmpty);
// print(fruitsDemo.length);
// print(fruitsDemo.keys);
// print(fruitsDemo.values);

// print(fruitsDemo.containsKey("apple"));
// print(fruitsDemo.containsValue("strawberry"));

// for(var a in fruitsDemo.keys)
// {
// print("Key : $a");
// }
// print("\n");
// for(var b in fruitsDemo.values)
// {
// print("Value : $b");
// }
// print("\n");
// for(var allkeys in fruitsDemo.keys)
// {
// print("${allkeys} : ${fruitsDemo[allkeys]}");
// }

// for(var c in fruitsDemo.entries)
// {
// print("${fruitsDemo.keys} : ${fruitsDemo.values}");
// }

fruitsDemo.forEach((key, value) {
// print("${fruitsDemo.keys} : ${fruitsDemo.values}");
// print("\n");
print("$key : $value");
});

}

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