Dart program to update single or multiple items of a LinkedHashMap

How to update the items of a LinkedHashMap in Dart:

In this post, we will learn how to update the items of a LinkedHashMap in Dart with different ways. The Dart class provides two methods to update a single item or multiple items of a LinkedHashMap. Let’s learn how these methods works with examples.

Update a single item with the update method:

The update method updates a single item of the LinkedHashMap. This method is defined as:

update(K key, V update(V value), {V ifAbsent()?})V
  • It updates the key with the provided value.
  • It will update the value of the key if it is present in the LinkedHashMap. Else, it throws Invalid argument (key) exception.
  • If we want to add the key, we have to provide the ifAbsent function as the third parameter which returns the value for the key.
  • This method returns the updated value.

Let’s take a look at the below example:

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 2: 'Banana', 3: 'Orange'});

  print('Original LinkedHashMap: ${linkedHMap}');

  linkedHMap.update(1, (value) => 'Apple');

  print('Updated LinkedHashMap: ${linkedHMap}');
}

In this example, we are updating the value of the key 1. It will print:

Original LinkedHashMap: {1: Mango, 2: Banana, 3: Orange}
Updated LinkedHashMap: {1: Apple, 2: Banana, 3: Orange}

If I try to update the value of an invalid key, it will throw an exception:

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 2: 'Banana', 3: 'Orange'});

  print('Original LinkedHashMap: ${linkedHMap}');

  linkedHMap.update(4, (value) => 'Apple');

  print('Updated LinkedHashMap: ${linkedHMap}');
}

It will throw Invalid argument exception: LinkedHashMap update error

Update multiple items of a LinkedHashMap:

To update multiple items of a LinkedHashMap, we have to use the updateAll method. The syntax of this method is:

 updateAll(V update(K key, V value))void

It takes one function as the parameter and updates all the pairs defined by this function. It iterates over the elements of the LinkedHashMap and updates them using the update function.

For example,

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 2: 'Banana', 3: 'Orange'});

  print('Original LinkedHashMap: ${linkedHMap}');

  linkedHMap.updateAll((key, value) => value.toUpperCase());

  print('Updated LinkedHashMap: ${linkedHMap}');
}

It will update the values of the LinkedHashMap to upperCase.

Original LinkedHashMap: {1: Mango, 2: Banana, 3: Orange}
Updated LinkedHashMap: {1: MANGO, 2: BANANA, 3: ORANGE}

We can use this method to add a check with the keys. For example, the below example will change the value of the even keys to uppercase:

import 'dart:collection';

String update(key, value) {
  return key % 2 == 0 ? value.toUpperCase() : value;
}

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 2: 'Banana', 3: 'Orange'});

  print('Original LinkedHashMap: ${linkedHMap}');

  linkedHMap.updateAll(update);

  print('Updated LinkedHashMap: ${linkedHMap}');
}

It will give:

Original LinkedHashMap: {1: Mango, 2: Banana, 3: Orange}
Updated LinkedHashMap: {1: Mango, 2: BANANA, 3: Orange}

You might also like: