3 ways to add items to a LinkedHashMap in Dart

How to add items to a LinkedHashMap in Dart:

There are different methods available to add items to a LinkedHashMap in Dart. In this post, we will learn how to use these methods with examples for each.

Method 1: By using the addAll method:

The addAll method is used to add all items of one map to another map. The syntax of this method is:

void addAll(Map<K, V> o)

It will add all the pairs of the Map o to the current LinkedHashMap. If any key of the parameter Map already exists in the current LinkedHashMap, it will overwrite the value for that key.

For example,

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.identity();
  final map = <int, String>{1: 'Apple', 2: 'Orange', 3: 'Mango'};

  linkedHMap.addAll(map);

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

This program added the content of the map to the LinkedHashMap linkedHMap. If you run this program, it will print:

LinkedHashMap: {1: Apple, 2: Orange, 3: Mango}

As you can see here, the items are added to the LinkedHashMap.

Now let’s see how it updates the existing values. As I’ve mentioned before, it will replace the value if the key is already present in the LinkedHashMap. For the following example,

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 4: 'Banana'});
  final map = <int, String>{1: 'Apple', 2: 'Orange', 3: 'Mango'};

  linkedHMap.addAll(map);

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

It will change the value of key 1 as it was already in the LinkedHashMap. If you run this program, it will print:

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

Also, the order of the pairs is not changed.

Method 2: How to add entries to a LinkedHashMap:

We can use the addEntries method to add an iterable of entries to a LinkedHashMap. The syntax of the method is:

addEntries(Iterable<MapEntry<K, V>> entries)void

It takes one parameter, an iterable of MapEntry objects. It doesn’t return anything, i.e. its return type is void.

Let’s change the above example to use the addEntries method:

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.identity();
  final map = <int, String>{1: 'Apple', 2: 'Orange', 3: 'Mango'};

  linkedHMap.addEntries(map.entries);

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

It will print:

LinkedHashMap: {1: Apple, 2: Orange, 3: Mango}

Similar to the addAll method, this method also overwrites the value if the key is already in the LinkedHashMap. For example,

import 'dart:collection';

void main() {
  final linkedHMap = LinkedHashMap.from({1: 'Mango', 4: 'Banana'});
  final map = <int, String>{1: 'Apple', 2: 'Orange', 3: 'Mango'};

  linkedHMap.addEntries(map.entries);

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

It will overwrite the value ‘Mango’ with ‘Apple’ for key 1 since the key already exists in the LinkedHashMap. It will print:

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

Method 3: Add an entry if the key is not in the LinkedHashMap:

The map class provides a method called putIfAbsent that can be used to check if a key exists in a Map or not and add a pair if it doesn’t exist. Its syntax is:

V putIfAbsent(K key, V ifAbsent());
  • The first parameter key is the key to look for in the LinkedHashMap.
  • If the key is found, it will return the value associated with that key.
  • Else, it uses the ifAbsent method to find the value and adds the key-value pair to the LinkedHashMap. It will return the new value.

For example,

import 'dart:collection';

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

  final returnedValue = linkedHMap.putIfAbsent(5, () => 'Orange');

  print('Returned value: ${returnedValue}, LinkedHashMap: ${linkedHMap}');
}

It will find the key 5 in the LinkedHashMap linkedHMap. As this key is not there, it will add the key-value pair (5, ‘Orange’) to the LinkedHashMap. If you run this program, it will print:

Returned value: Orange, LinkedHashMap: {1: Mango, 4: Banana, 5: Orange}

The returned value of putIfAbsent is ‘Orange’, the value of the new key-value pair.

If the key is found in the LinkedHashMap, it will return its value:

import 'dart:collection';

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

  final returnedValue = linkedHMap.putIfAbsent(1, () => 'Orange');

  print('Returned value: ${returnedValue}, LinkedHashMap: ${linkedHMap}');
}

It will print:

Returned value: Mango, LinkedHashMap: {1: Mango, 4: Banana}

It didn’t modify the LinkedHashMap.

You might also like: