Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

splaytreemap
dart
communitycreator

How to remove all entries of the SplayTreeMap in Dart

Programming Bytes

Overview

We can use the clear() method to remove all entries of the SplayTreeMap.

After calling the clear method, the SplayTreeMap becomes empty.

Syntax

void clear()

Argument

This method doesn’t take any argument.

Return value

This method doesn’t return any argument.

Code

The code below demonstrates the use of the clear() method to remove all entries of SplayTreeMap:

import 'dart:collection';
void main() {
  //create a new SplayTreeMap which can have int key and string values
  SplayTreeMap map = new SplayTreeMap<int, String>((keya,keyb) =>  keyb.compareTo(keya));

  // add five entries to the map
  map[5] = "Five";
  map[4] = "Four";
  map[2] = "Two";
  map[1] = "One";
  map[3] = "Three";

  print('The map is $map');

  map.clear();
  print('\nThe map after calling clear method : $map');

}
Using the clear method

Explanation

  • Line 1: We import the collection library.

  • Line 4: We create a new SplayTreeMap object named map. We pass a compare function as an argument. This function is used for maintaining the order of the map entries. In our case, the compare function orders the elements in descending order.

  • Lines 7 to 11: We add five new entries to the map. Now, the map is {5=Five,4=Four,3=Three,2=Two,1=One}.

  • Line 15: We use the clear() method. This remove all the entries of the map. Now the map becomes empty.

RELATED TAGS

splaytreemap
dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring