Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What are HashMap.entries in Dart?

Programming Bytes

HashMap is a Hash-table-based implementation of MapMap contains a list of key-value pairs as elements.. Read more about HashMap here.

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 value
  HashMap map = new HashMap<String, int>();
  
  // add two entries to the map
  map["one"] =  1;
  map["two"] =  2;
  print('\nThe map is $map');
  
  // use entries property to get the entries of the map
  Iterable<MapEntry<String,int>> entries = map.entries;
  

  // using forEach and print the iterable
  print('\nPrinting entries Using forEach');
  entries.forEach((entry){
    var key = entry.key;
    var value = entry.value;
    print('$key - $value');
  }) ;

}
Using the entries property to get all the entries of the map

Explanation

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.

RELATED TAGS

dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring