Skip to main content

Flutter Dart HashMap Example

HashMap creates an unordered key map data object. The order of items can specified later and can also be in any order. For example we can add item at index 10 without adding items on before index 10. If we add item on index 7 later then it will automatically arrange the HashMap after assigning value.

Flutter Dart HashMap Example

1. Import dart:collection package.
import 'dart:collection'; `
2. Defining a HashMap for Integer and String value.
final Map<int, String> countTable = HashMap();
3. Adding Items in HashMap on random index position.
  countTable[3] = 'Three';
  countTable[1] = 'One';
4. Adding item in HashMap with Index position included.
countTable.addAll({4: 'Four'});
5. Add multiple data with multiple index positions.
final tempData = {6: 'Six', 5: 'Five'};
  countTable.addEntries(tempData.entries);
Source code for main.dart file:
import 'dart:collection'; 
void main() { 
  final Map<int, String> countTable = HashMap();
  countTable[3] = 'Three';
  countTable[1] = 'One';
  print(countTable);
  
  countTable.addAll({4: 'Four'});
  print(countTable);
  
  final tempData = {6: 'Six', 5: 'Five'};
  countTable.addEntries(tempData.entries);
  print(countTable);
}
Output:
Flutter Dart HashMap Example

Comments