What is the LinkedHashMap.isEmpty in Dart?

Overview

LinkedHashMap is a hash-table based on mapMap contains a list of key-value pairs as an element.. The LinkedHashMap maintains the insertion order of the entries. Read more about LinkedHashMap here.

We can use the isEmpty property to check whether the map is empty (there is no key-value pair present in the LinkedHashMap).

Syntax

map.isEmpty

Return value

This property returns true if the LinkedHashMap is empty. Otherwise, false is returned.

Code

The below code demonstrates how to check if the LinkedHashMap is empty:

import 'dart:collection';
void main() {
//create a new LinkedHashMap which can have string type as key, and int type as value
LinkedHashMap map = new LinkedHashMap<String, int>();
print('The map is $map');
// use isEmpty property to check if the map is empty
print('map.isEmpty : ${map.isEmpty}');
// add two entries to the map
map["one"] = 1;
map["two"] = 2;
print('\nThe map is $map');
// use isEmpty property to check if the map is empty
print('map.isEmpty : ${map.isEmpty}');
}

Explanation

In the above code,

  • Line 4: We create a new LinkedHashMap object with map.

  • Line 8: We use the isEmpty property to check if the map is empty. In our case, the map is empty, so true is returned.

  • Lines 11 and 12: We add two new entries to the map. Now the map is {one: 1, two: 2}.

  • Line 16: We use the isEmpty property to check if the map is empty. In our case, the map contains two entries and is not empty, so false is returned.