Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clear
dart
communitycreator

LinkedHashMap's containsKey and containsValue method in Dart

Programming Bytes

A LinkedHashMap is a hash-table based implementation of MapMap contains a list of key-value pairs as an element.. The LinkedHashMap maintains the insertion order of entries. We can read more about the concept of a LinkedHashMap here.

The containsKey method is used to check if the LinkedHashMap contains a specific key.

The containsValue method is used to check if the LinkedHashMap contains a specific value.

Synax

// check if the key is present
bool containsKey(Object? key)

// check if the value is present
bool containsValue(Object? value)

Argument

The containsKey method takes the key whose presence needs to be checked in the LinkedHashMap as an argument.

The containsValue method takes the value whose presence needs to be checked in the LinkedHashMap, as an argument.

Return value

This method returns True if the passed key/value is present in the LinkedHashMap.

Code

The code written below demonstrates how we can use the containsKey and containsValue method of the LinkedHashMap:

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>();

  // add two entries to the map
  map["one"] =  1;
  map["two"] =  2;

  print('The map is $map');
  // Using the "containsKey"  mehod to check if the map has a specific key
  print('\nmap.containsKey("one") is ${map.containsKey("one")}');
  print('map.containsKey("three") is ${map.containsKey("three")}');

  // Using the "containsValue"  mehod to check if the map has a specific value
  print('\nmap.containsValue(1) is ${map.containsValue(1)}');
  print('map.containsValue(3) is ${map.containsValue(3)}');
}
Using the "containsKey" and "containsValue" method

Explanation

In the code written above:

  • In line 1, we import the collection library.

  • In line 4, we create a new LinkedHashMap object with the name map.

  • In lines 7 and 8, we add two new entries to the map.

  • In line 12, we use the containsKey method with one as an argument. This will check if the map has an entry with the key one. In our case, there is an entry with the key one, so True is returned.

  • In line 13, we use the containsKey method with three as an argument. This will check if the map has an entry with the key three. In our case, there is no entry with the key three in the map, so False is returned.

  • In line 16, we use the containsValue method with 1 as an argument. This will check if the map has an entry with the value 1. In our case, there is an entry with the value 1, so True is returned.

  • In line 17, we use the containsValue method with 3 as an argument. This will check if the map has an entry with the value 3. In our case, there is no entry with the value 3 in the map, so False is returned.

RELATED TAGS

clear
dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring