How to add and remove items of a HashMap in Dart

How to add and remove items of a HashMap in Dart:

In this post, we will learn how to add and remove items from a HashMap in Dart. There are different ways to add and remove items from a Dart HashMap.

How to add items to a HashMap with a square bracket operator:

We can use the [] operator to add key-value pairs to a HashMap. We need to put the key in the operator and the value can be assigned with an equal operator.

For example:

import 'dart:collection';

void main() {
  final Map<int, String> hashMap = HashMap();

  hashMap[1] = "one";
  hashMap[2] = "two";
  hashMap[3] = "three";
  hashMap[4] = "four";
  hashMap[5] = "five";

  print("HashMap: ${hashMap}");
}

It created one empty HashMap and inserted five key-value pairs into the HashMap. It will print:

HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}

Add items with addAll():

The addAll method takes one Map as its parameter and adds the key-value pairs of the Map to the HashMap.

addAll(Map<K, V> o)void

This method is useful to add multiple key-value pairs to a HashMap in one go. For example,

import 'dart:collection';

void main() {
  final Map<int, String> hashMap = HashMap();

  hashMap.addAll({1: "one", 2: "two", 3: "three", 4: "four", 5: "five"});

  print("HashMap: ${hashMap}");
}

It will print:

HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}

Add items with addEntries:

With this method, we can add the pairs of a given entries. This is defined as:

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

For example,

import 'dart:collection';

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addEntries(data.entries);

  print("HashMap: ${hashMap}");
}

It will print: Dart example addEntries

putIfAbsent:

This method adds one pair if the provided key is not found in the HashMap. Else, it returns the pair found in the HashMap.

V putIfAbsent(K key, V ifAbsent())

The ifAbsent is a method to get the value for the key. It will add the pair if the key is not found in the HashMap.

import 'dart:collection';

String ifAbsent() {
  return "six";
}

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addAll(data);
  final returnValue = hashMap.putIfAbsent(6, ifAbsent);

  print("Return value: ${returnValue}, HashMap: ${hashMap}");
}

In this example, since 6 is not in the HashMap, it will add the pair {6, “six”} to it. The return value of putIfAbsent is “six”.

It will print:

Return value: six, HashMap: {1: one, 2: two, 3: three, 4: four, 5: five, 6: six}

Now, if we try to add {5, “six”} to the hashmap, it will fail:

import 'dart:collection';

String ifAbsent() {
  return "six";
}

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addAll(data);
  final returnValue = hashMap.putIfAbsent(5, ifAbsent);

  print("Return value: ${returnValue}, HashMap: ${hashMap}");
}

It will fail to add as 5 is already there in the hashmap. The returned value will be ‘five’, i.e. the value of the key 5 in the hashmap.

It will print:

Return value: five, HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}

How to remove a single object from the HashMap:

We can use the remove method to remove a single object from a HashMap. This method takes the key as its parameter and removes the key-value pair if the key is found in the HashMap.

remove(Object? k)V?

It returns the value.

import 'dart:collection';

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addAll(data);
  print("Original HashMap: ${hashMap}");

  final removedValue = hashMap.remove(5);

  print("Removed value: ${removedValue}, HashMap: ${hashMap}");
}

It will remove the pair with key 5 from the HashMap.

Output:

Original HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}
Removed value: five, HashMap: {1: one, 2: two, 3: three, 4: four}

Remove multiple values from a HashMap with a given condition:

The removeWhere method takes one function and removes all elements for those it returns true.

removeWhere(bool f(K k, V v))void

For example,

import 'dart:collection';

bool canRemove(int key, String value) {
  return key % 2 == 0;
}

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addAll(data);
  print("Original HashMap: ${hashMap}");

  hashMap.removeWhere(canRemove);

  print("Final HashMap: ${hashMap}");
}

In this example, we are passing the canRemove function to the removeWhere method. It will remove all the pairs with a key with even value.

It will print:

Original HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}
Final HashMap: {1: one, 3: three, 5: five}

Remove all items of a HashMap:

The clear method removes all the items of a HashMap.

clear()void

Example:

import 'dart:collection';

void main() {
  final Map<int, String> hashMap = HashMap();
  final data = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five"};

  hashMap.addAll(data);
  print("Original HashMap: ${hashMap}");

  hashMap.clear();

  print("Final HashMap: ${hashMap}");
}

It will print:

Original HashMap: {1: one, 2: two, 3: three, 4: four, 5: five}
Final HashMap: {}

Reference:

You might also like: