5 ways to check if Dart LinkedHashMap contains a key or value

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}');
}

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: true

Method 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}');
}

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}');
}

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: true

Method 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) → bool

This 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.

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}');
}

It returns one boolean value similar to the previous method. Output:

isAvailable: true

You might also like: