How to add and remove items from a HashSet in Dart

How to add and remove items from a HashSet in Dart:

The HashSet is a hash-table based set implementation. This post will show you how to add and remove items from a HashSet in different ways.

Add elements to a HashSet:

We have two different methods to add single or multiple elements to a HashSet:

  • add(E e) → bool
  • addAll(Iterable<E> it) → void

Add a single element to HashSet with the add method:

The add method adds a single element to the HashSet. This is defined as:

add(E e) → bool

This method adds one single element to the HashSet. It returns one boolean value, true if the element is added, else false.

For example,

import 'dart:collection';

void main() {
  final hashSet = HashSet<String>();

  final vowels = ['A', 'E', 'I', 'O', 'U', 'E'];

  for (var e in vowels) {
    if (hashSet.add(e)) {
      print('Added ${e}');
    } else {
      print('Failed to add ${e}');
    }
  }

  print('Final HashSet: ${hashSet}');
}

In this example, we are using a loop to add the strings of vowels to the HashSet. Based on the return value of add(), it prints a message to the user.

Output:

Added A
Added E
Added I
Added O
Added U
Failed to add E
Final HashSet: {A, O, I, E, U}

It failed to add E because it was already added to the HashSet. HashSet is unordered. Hence the final HashSet is not following the order of data insertion.

Add multiple items to a HashSet with addAll:

The addAll method is defined as:

addAll(Iterable<E> it)void

It adds all the items of the iterable it to the HashSet.

import 'dart:collection';

void main() {
  final hashSet = HashSet<String>();

  final vowels = ['A', 'E', 'I', 'O', 'U', 'E'];

  hashSet.addAll(vowels);

  print('Final HashSet: ${hashSet}');
}

It will print:

Final HashSet: {A, O, I, E, U}

Remove elements from a HashSet:

The following methods are available to remove and retain specific elements of a HashSet.

  • remove(Object? o) → bool
  • removeAll(Iterable<Object?> it) → void
  • clear() → void
  • removeWhere(bool fn(E e)) → void
  • retainWhere(bool fn(E e)) → void
  • retainAll(Iterable<Object?> it) → void

Remove a single element:

We can use the remove method to remove a single element from a HashSet. This method takes one object as its parameter and removes the object from HashSet. It returns one boolean value.

remove(Object? o) → bool

Let’s try this method with an example:

import 'dart:collection';

void main() {
  final hashSet = HashSet<String>();

  final vowels = ['A', 'E', 'I', 'O', 'U'];

  hashSet.addAll(vowels);

  for (var e in ['A', 'B']) {
    if (hashSet.remove('A')) {
      print('${e} is removed');
    } else {
      print("${e} can't be removed");
    }
  }

  print('Final HashSet: ${hashSet}');
}

In this example, we are trying to remove the strings of [‘A’, ‘B’] from the HashSet hashSet. Based on its return value, it prints one message.

It will give the below output:

A is removed
B can't be removed
Final HashSet: {O, I, E, U}

Remove multiple elements from a HashSet:

There is a method called removeAll to remove multiple elements from a HashSet. This method takes one iterable and removes all elements of the iterable from the HashSet.

removeAll(Iterable<Object?> it)void

For example,

import 'dart:collection';

void main() {
  final hashSet = HashSet<String>();

  final vowels = ['A', 'E', 'I', 'O', 'U'];

  hashSet.addAll(vowels);

  hashSet.removeAll(['A', 'U', 'B']);

  print('Final HashSet: ${hashSet}');
}

It will print:

Final HashSet: {O, I, E}

Remove all elements of a HashSet:

The clear() method removes all the elements of a HashSet.

clear()void

Example:

import 'dart:collection';

void main() {
  final hashSet = HashSet<String>();

  final vowels = ['A', 'E', 'I', 'O', 'U'];

  hashSet.addAll(vowels);

  hashSet.clear();

  print('Final HashSet: ${hashSet}');
}

Output:

Final HashSet: {}

Remove and retain all the elements with a function:

There are two methods removeWhere and retainWhere can be used to remove and retain all the elements of a HashSet with a given function.

  • removeWhere(bool fn(E e)) → void
  • retainWhere(bool fn(E e)) → void

For example,

import 'dart:collection';

void main() {
  final hashSet = HashSet<int>();

  final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  hashSet.addAll(numbers);

  hashSet.removeWhere((e) => e % 2 == 0);

  print('Final HashSet: ${hashSet}');
}

The function passed to the removeWhere method is checking if the current element is even or not, i.e. it will remove all the even numbers from the HashSet.

Output:

Final HashSet: {1, 3, 5, 7, 9}

If we replace removeWhere with retainWhere, it will keep the even numbers and remove the odd numbers.

Retain all the elements of an iterable:

There is one method called retainAll to retain all the elements of a given iterable. We need to pass one iterable to this method and it will remove the elements those are not in the iterable.

retainAll(Iterable<Object?> it)void

Example program:

import 'dart:collection';

void main() {
  final hashSet = HashSet<int>();

  final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

  hashSet.addAll(numbers);

  hashSet.retainAll([1, 2, 5, 6, 7, 20]);

  print('Final HashSet: ${hashSet}');
}

It will print:

Final HashSet: {1, 2, 5, 6, 7}

You might also like: