A LinkedHashMap is a
hash-table
based implementation of. The LinkedHashMap maintains the insertion order of entries. We can read more about the concept of a Map Map contains a list of key-value pairs as an element. 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.
// check if the key is present
bool containsKey(Object? key)
// check if the value is present
bool containsValue(Object? value)
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.
This method returns True
if the passed key/value is present in the LinkedHashMap.
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 valueLinkedHashMap map = new LinkedHashMap<String, int>();// add two entries to the mapmap["one"] = 1;map["two"] = 2;print('The map is $map');// Using the "containsKey" mehod to check if the map has a specific keyprint('\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 valueprint('\nmap.containsValue(1) is ${map.containsValue(1)}');print('map.containsValue(3) is ${map.containsValue(3)}');}
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.