Dart Programming – Map

The map is the datatype which is used to represent a set of elements in lock-key pair. In the map, both the key and the lock can be of any type and the key and the lock don’t need to be of the same type they can be of different types. It must be noted that all the keys are unique and can’t be repeated for the same map whereas values can be repeated.
We can declare Map in two ways: 

  1. Using Map Literals
  2. Using Map Constructors

Map Literals:

Map can be declared using map literals as shown below: 

Syntax: 
// Creating the Map using Map Literals
var map_name = { key1 : value1, key2 : value2, ..., key n : value n }

Example 1: 

Creating Map using Map Literals 
 

Dart



filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using is literals
  var gfg = {'position1' : 'Geek', 'position2' : 'for', 'position3' : 'Geeks'};
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg['position1']); 
    
  // Key is not defined
  print(gfg[0]); 
}

chevron_right


Output: 

{position1: Geek, position2: for, position3: Geeks}
Geek
null

Example 2: 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using is literals
  var gfg = {'position1' : 'Geek' 'for' 'Geeks'};
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg['position1']); 
}

chevron_right


Output: 

{position1: GeekforGeeks}
GeekforGeeks

You have noticed that different strings get concatenated to one.

Example 3: 

Inserting a new value into Map 
 

Dart



filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map
  var gfg = {'position1' : 'Geeks' 'for'  'Geeks'};
    
  // Printing Its content before insetion
  print(gfg);
    
  // Inserting a new vaplue in Map
  gfg ['position0'] = 'Welcome to ';
    
  // Printing Its content after insertion
  print(gfg);
    
  // Printing Specific Content
  // Keys is defined
  print(gfg['position0'] + gfg['position1']); 
}

chevron_right


Output: 

{position1: GeeksforGeeks}
{position1: GeeksforGeeks, position0: Welcome to }
Welcome to GeeksforGeeks

Map Constructors:

Syntax: 
// Creating the Map using Map Constructor
var map_name = new Map();
// Assigning value and key inside Map
map_name [ key ] = value;

Example 1:

Creating Map using Map Constructors 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using Contructors
  var gfg = new Map();
    
  // Inserting values into Map
  gfg [0] = 'Geeks';
  gfg [1] = 'for';
  gfg [2] = 'Geeks';
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg[0]); 
}

chevron_right


Output: 

{0: Geeks, 1: for, 2: Geeks}
Geeks

Example 2: 

Assigning same key to different element 
 

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

void main() {  
  // Creating Map using Contructors
  var gfg = new Map();
    
  // Inserting values into Map
  gfg [0] = 'Geeks';
  gfg [0] = 'for';
  gfg [0] = 'Geeks';
    
  // Printing Its content
  print(gfg);
    
  // Printing Specific Content
  // Key is defined
  print(gfg[0]); 
}

chevron_right


Output: 

{0: Geeks}
Geeks

You have noticed that the other two values were simply ignored.
 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.