A map is a collection in which data can exist in the form of key, and value pairs. retrieve of a value can be handled based upon the associated key. In the map, a key should be unique, whereas duplicate records are allowed for the value. a Map is broadly classified into three categories based on the iteration order they are
Hashmap which is unordered.
LinkedHashmap which iterates as per the key insertion order.
Splay Treemap which iterated the keys in sorted order.
There are two approaches to defining a map
via Map Literals
via Map Constructor
Let's discuss the above declaration types in detail
A map literal consists of a pair of curly brackets, in which we specify the key/value pairs. These pairs are separated by a comma ( , ). The key is separated from the value by a colon ( : ).
Syntax:
var mapLiteral = { Key1: Value1, Key2: Value2, Key3: Value3, ........., Key_n: Value_n}
Eg:
void main(){
var studentInfo = {'Name': 'Aditya', 'Age': 23, 'Job': 'Student'};
print(studentInfo);
}
// Output: {Name: Aditya, Age: 23, Job: Student}
It can be implemented in two ways. Initially, we need to declare a map using the map() constructor and then we initialize the map. Please go through the syntax given below
Syntax:
var map_initialization = Map();
var map_initialization['Key'] = value;
Eg:
void main(){
// Map initialization with constructor
var userInfo = Map();
userInfo['Name'] = 'Aditya';
userInfo['Age'] = '34';
userInfo['Gender'] = 'Male';
print(userInfo);
// Output : {Name: Aditya, Age: 34, Gender: Male}
}
Let's take a look at other items that are available in Maps.
when we want to add entries to an existing Map object we adopt this method
Eg:
void main(){
var countries = {1: 'India', 2: 'USA', 3: 'Canada', 4: 'Japan', 5: 'Israel'};
print(countries);
countries.addAll({6: 'Uk', 7: 'Australia'});
print(countries);
// Output: {1: India, 2: USA, 3: Canada, 4: Japan, 5: Israel, 6: Uk, 7: Australia}
}
When we want to clear data from Map, we use this method.
Eg:
void main(){
var countries = {1: 'India', 2: 'USA', 3: 'Canada', 4: 'Japan', 5: 'Israel'};
countries.clear();
// Data after clearing from Map
print(countries);
// Output: {}
}
It helps in checking whether given key is available or not and returns a boolean value.
Eg:
void main(){
var countries = {'India': 'New Delhi', 'USA': 'Washington DC', 'Canada': 'Ottawa', 'Japan' : 'Tokyo', 'Israel' :'Jerusalem' };
print('Is India Available : ${countries.containsKey('India')}');
// Output: Is India Available : true
print('Is Nepal Available : ${countries.containsKey('Nepal')}');
// Output: Is Nepal Available : false
}
For checking value we use this method
Eg:
void main(){
var countries = {'India': 'New Delhi', 'USA': 'Washington DC', 'Canada': 'Ottawa', 'Japan' : 'Tokyo', 'Israel' :'Jerusalem' };
print('Is New Delhi is the capital of India : ${countries.containsValue('New Delhi')}');
// Output: Is New Delhi is the capital of India : true
print('Is London is the capital of Germany : ${countries.containsValue('London')}');
// Output: Is London is the capital of Germany : false
}
This method is useful for loading each and every key values that are available in the given map.
Eg:
void main(){
var countries = {'India': 'New Delhi', 'USA': 'Washington DC', 'Canada': 'Ottawa', 'Japan' : 'Tokyo', 'Israel' :'Jerusalem' };
countries.forEach((key, value){
print('Key : '+ key+' Value : '+value);
});
// Output
// Key : India Value : New Delhi
// Key : USA Value : Washington DC
// Key : Canada Value : Ottawa
// Key : Japan Value : Tokyo
// Key : Israel Value : Jerusalem
}
Removes the corresponding value when it satisfies the condition
Eg:
void main(){
var countries = {'India': 'New Delhi', 'USA': 'Washington DC', 'Canada': 'Ottawa', 'Japan' : 'Tokyo', 'Israel' :'Jerusalem' };
print(countries);
var removeCanada = countries.removeWhere((key, value) => value.startsWith('O') );
print(countries);
}
Note: other alternative methods for removing a value along with startsWith() are contains() & endsWith().
// Output : {India: New Delhi, USA: Washington DC, Japan: Tokyo, Israel: Jerusalem}
When we want to update the value of a key instead of removing and re adding we can use this method.
Eg:
void main(){
var countries = {'India': 'New Delhi', 'USA': 'Washington DC', 'Australia': 'Sydney', 'Canada': 'Ottawa', 'Japan' : 'Tokyo', 'Israel' :'Jerusalem' };
var updateAustralia = countries.update('Australia', (value) => 'Canberra' );
print(countries);
// Output : {India: New Delhi, USA: Washington DC, Australia: Canberra, Canada: Ottawa, Japan: Tokyo, Israel: Jerusalem}
}