What are HashMap.entries in Dart?
HashMapis a Hash-table-based implementation of. Read more about Map Map contains a list of key-value pairs as elements. HashMaphere.
The entries property can be used to get all entries (key-value pairs) of the map.
Syntax
Iterable<MapEntry<K, V>> get entries;
Return value
This property returns an iterable which contains all the key-value pairs of the map.
Code
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');}) ;}
Explanation
In the code above,
-
In line 4, we create a new
HashMapobject with the namemap. -
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
entriesproperty to get all the entries of themap. We will get the entries as anIterableobject. -
In lines 17-21, we use the
forEachmethod of theentriesiterable and print all the entries ofIterable.