LinkedHashMap is an implementation of
, which is based on a hash table. The LinkedHashMap maintains the insertion order of the entries. we can read more about the concept of a Map Map contains a list of key-value pairs as an element. LinkedHashMap
here.
The keys
property can be used to get all the keys of the LinkedHashMap
in the insertion order.
Iterable<K> keys
This property returns an iterable that contains all the keys of the LinkedHashMap
. The iterable contains the keys in the insertion order.
The entries
property can be used to get all the entries (key-value pairs) of the LinkedHashMap
.
Iterable<MapEntry<K, V>> get entries;
This property returns an iterable that contains all the key-value pairs of the LinkedHashMap
in the insertion order.
Note: Modifying the map while iterating the keys may result in an unhandled exception.
The code below demonstrates how we can get all the keys and entries of a map:
import 'dart:collection';void main() {//create a new LinkedHashMap which can have string type as key, and int type as valueLinkedHashMap map = new LinkedHashMap<String, int>();// add two entries to the mapmap["one"] = 1;map["two"] = 2;print('\nThe map is $map');// use keys property to get the keys of the mapIterable<String> keys = map.keys;print('map.keys: $keys');// use entries property to get the entries of the mapIterable<MapEntry<String,int>> entries = map.entries;print('map.entries: $entries');}
Line 4: We create a new LinkedHashMap
object with the name map
.
Lines 7–8: We add two new entries to the map
. Now, the map is {one: 1, two: 2}
.
Line 12: We use the keys
property to get all the keys of the map
. We will get the keys as an Iterable
object. When we print the returned iterable, we will get (one, two)
.
Line 16: We use the entries
property to get all the entries of the map
. We will get the entries as an Iterable
object.