Dart program to check if a LinkedHashMap contains a key or value:
The LinkedHashMap class is a hash table-based Map implementation that remembers the insertion order of the key-value pairs. It provides a couple of different properties and methods. In this post, I will show you four different ways to check if a LinkedHashMap contains a key or value with examples.
Method 1: Find in the iterable of keys if the LinkedHashMap contains a key:
The keys property of a LinkedHashMap returns an iterable of keys of the LinkedHashMap. We can use this iterable to find out if a specific key is in the LinkedHashMap or not. The syntax of the keys property is:
keys → Iterable<K>Let’s try it with an example:
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from(
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'});
final key = 5;
final isAvailable = linkedHMap.keys.any((element) => element == key);
print('isAvailable: ${isAvailable}');
}- Download it on Github
We are using the any method to check if the key is found in the iterable returned by the keys property. The return value is printed at the end of the program.
Output:
isAvailable: trueMethod 2: Find in the iterable of values if the LinkedHashMap contains a value:
Similar to the above example, we have another property of LinkedHashMap, values, to get all the values of a LinkedHashMap. The following example shows how to check if a value is available in a given LinkedHashMap or not:
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from(
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'});
final value = 'six';
final isAvailable = linkedHMap.values.any((element) => element == value);
print('isAvailable: ${isAvailable}');
}- Download it on Github
It will also print true as the value ‘six’ is in the given LinkedHashMap.
Method 3: Check if both key and value exist in a LinkedHashMap:
The entries property of LinkedHashMap returns an iterable of MapEntry objects those hold both key and value. We can use this property to check if a LinkedHashMap contains a given key-value pair.
The syntax of the entries property is:
entries → Iterable<MapEntry<K, V>>Example program:
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from(
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'});
final value = 'six';
final key = 6;
final isAvailable = linkedHMap.entries
.any((entry) => entry.key == key && entry.value == value);
print('isAvailable: ${isAvailable}');
}- Download it on Github
The example is comparing both key and value with each item of the entries. The any method returns a boolean value if an item is found. It will print:
isAvailable: trueMethod 4: By using the containsKey method:
There is one method called containsKey is available to check if a LinkedHashMap contains a key or not.
containsKey(Object? key) → boolThis method takes a key and returns one boolean value. We can directly use this method to check if a key exists or not.
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from(
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'});
final key = 6;
final isAvailable = linkedHMap.containsKey(key);
print('isAvailable: ${isAvailable}');
}It prints true.
- Download it on Github
Method 5: By using the containsValue method:
Similar to the containsKey method, there is another method called containsValue to check if a value exists in a LinkedHashMap or not.
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from(
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'});
final value = 'five';
final isAvailable = linkedHMap.containsValue(value);
print('isAvailable: ${isAvailable}');
}- Download it on Github
It returns one boolean value similar to the previous method. Output:
isAvailable: trueYou might also like:
- Dart HashSet skip and skipWhile methods explanation with example
- Introduction to Dart LinkedHashMap class
- 7 ways to create a LinkedHashMap in Dart
- 3 ways to add items to a LinkedHashMap in Dart
- 3 ways to remove items to a LinkedHashMap in Dart
- Dart program to update single or multiple items of a LinkedHashMap
