Dart Map
Dart Map
Dart Map is an object that stores data in the form of a key-value pair. Each value is associated with its key, and it is used to access its corresponding value. Both keys and values can be any type. In Dart Map, each key must be unique, but the same value can occur multiple times. The Map representation is quite similar to Python Dictionary. The Map can be declared by using curly braces {} ,and each key-value pair is separated by the commas(,). The value of the key can be accessed by using a square bracket([]).
Declaring a Dart Map
Dart Map can be defined in two methods.
- Using Map Literal
- Using Map Constructor
The syntax of declaring Dart Map is given below.
Using Map Literals
To declare a Map using map literal, the key-value pairs are enclosed within the curly braces "{}" and separated by the commas. The syntax is given below.
Syntax -
Example - 1:
Output:
{name: Tom, age: 23}
Example - 2: Adding value at runtime
Output:
{name: tom, age: 23, course: B.tech}
Explanation -
In the above example, we declared a Map of a student name. We added the value at runtime by using a square bracket and passed the new key as a course associated with its value.
Using Map Constructor
To declare the Dart
Map using map constructor can be done in two ways. First, declare a map using map() constructor. Second, initialize the map. The syntax is given below.Syntax -
After that, initialize the values.
Example - 1: Map constructor
Output:
{name: Tom, age: 23, course: B.tech, Branch: Computer Science}
Note - A map value can be any object including NULL.
Map Properties
The dart:core:package has Map class which defines following properties.
Properties | Explanation |
---|---|
Keys | It is used to get all keys as an iterable object. |
values | It is used to get all values as an iterable object. |
Length | It returns the length of the Map object. |
isEmpty | If the Map object contains no value, it returns true. |
isNotEmpty | If the Map object contains at least one value, it returns true. |
Example -
Output:
{name: Tom, age: 23, course: B.tech, Branch: Computer Science} The keys are : (name, age, course, Branch) The values are : (Tom, 23, B.tech, Computer Science) The length is : 4 false true
Map Methods
The commonly used methods are given below.
addAll() - It adds multiple key-value pairs of other. The syntax is given below.
Syntax -
Parameter:
- other - It denotes a key-value pair. It returns a void type.
Let's understand the following example.
Example -
Output:
Map :{name: Tom, age: 23} Map after adding key-values :{name: Tom, age: 23, dept: Civil, email: tom@xyz.com}
remove() - It eliminates all pairs from the map. The syntax is given below.
Syntax -
Let's have a look at following example.
Example -
Output:
Map :{name: Tom, age: 23} Map after removing all key-values :{}
remove() - It removes the key and its associated value if it exists in the given map. The syntax is given below.
Syntax -
Parameter -
- Keys - It deletes the given entries. It returns the value associated with the specified key.
Let's understand the following example.
Example -
Output:
Map :{name: Tom, age: 23} Map after removing given key :{name: Tom}
forEach() - It is used to iterate the Map's entries. The syntax is given below.
Syntax -
Output:
Map :{name: Tom, age: 23} name: Tom age: 23
Comments
Post a Comment