How to use the containsValue() method of SplayTreeMap in Dart

Overview

The containsValue() method checks if an entry for the specified value is present in the SplayTreeMap.

Syntax

bool containsValue(Object? value)

Parameters

This method takes the argument, value, which represents the value we want to check.

Return value

This method returns a Boolean value as a result. If the passed argument matches any of the values present in the SplayTreeMap object then true is returned. Otherwise, false will be returned.

Code

In the example below we use the containsValue method to check if a specific value is present in the 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');
// check if the map has a value One
print('map.containsValue("One") is ${map.containsValue("One")}');
// check if the map has a value Ten
print('map.containsValue("Ten") is ${map.containsValue("Ten")}');
}

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 maintains the map entries’. In our case, the compare function orders the elements in descending order.

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

Line 16: Calls the containsValue method. For this method, we pass One as an argument This will checks if the map object has an entry with One as a value. In our map object, there is one entry with the value One. So, true is returned.

Line 16: Calls the containsValue method. For this method, we pass Ten as an argument This will checks if the map object has an entry with Ten as a value. In our map object, there is no entry present for the value Ten. So, false is returned.