HashMap
is a Hash-table-based implementation of. Read more about Map Map contains a list of key-value pairs as elements. HashMap
here.
The entries
property can be used to get all entries (key-value pairs) of the map.
Iterable<MapEntry<K, V>> get entries;
This property returns an iterable which contains all the key-value pairs of the map.
The code below demonstrates how to get all the entries of a map.
import 'dart:collection';void main() {//create a new hashmap which can have string type as key, and int type as valueHashMap map = new HashMap<String, int>();// add two entries to the mapmap["one"] = 1;map["two"] = 2;print('\nThe map is $map');// use entries property to get the entries of the mapIterable<MapEntry<String,int>> entries = map.entries;// using forEach and print the iterableprint('\nPrinting entries Using forEach');entries.forEach((entry){var key = entry.key;var value = entry.value;print('$key - $value');}) ;}
In the code above,
In line 4, we create a new HashMap
object with the name map
.
In lines 7-8, we add two new entries to the map
. Now the map is {one: 1, two: 2}
In line 12, we use the entries
property to get all the entries of the map
. We will get the entries as an Iterable
object.
In lines 17-21, we use the forEach
method of the entries
iterable and print all the entries of Iterable
.