Dart/Flutter: How to Compare two Sets for equal Example

This tutorial shows multiple examples of how to compare two Sets in Dart and Flutter programming.

Two sets are equal if it matches the below criteria

  • Length of two sets is equal
  • Order of elements is not important
  • If the element is an object, The key and values of objects in both sets should be matched.

Check if two sets are equal or not

In dart, quiver🔗 package a library for collection utility reusable functions

setsEqual in collections function that compares two sets and returns true if equal, else not equal.

Syntax:

bool setsEqual(Set? a, Set? b)

It takes two Set for comparison Return value: true or false.

Here is an example code

import 'package:quiver/collection.dart';

void main() {
  var set1 = <int>{3, 7, 2};
  var set2 = {3, 7, 2};
  print(setsEqual(set1, set2)); // true

  var set3 = <int>{13, 71, 21};
  var set4 = {31, 71, 2};
  print(setsEqual(set3, set4)); // false
}


Output:

true
false

How to compare two Sets of numbers for equal or not in Flutter

If your application is of flutter, It has a foundation.dart library.

foundation.dart has a setEquals method, does compare each element by element in the Set, and returns an equal comparison result with a boolean type.

bool setEquals<T>(Set<T>? a, Set<T>? b)
  • true: Returns true if two Sets are equal with ignoring the order of elements.
  • false: Elements in the Set. Here, Compare the two Set of numbers and returns true if the two sets of values are equal.

Here is an example of equality for a Set of numbers.

import 'package:flutter/foundation.dart';

void main() {
  var set1 = <int>{3, 7, 2};
  var set2 = {3, 7, 2};
  print(setEquals(set1, set2)); // true

  var set3 = <int>{13, 71, 21};
  var set4 = {31, 71, 2};
  print(setEquals(set3, set4)); // false
}

How to compare two Sets of strings in a flutter

In the below example, set1 and set 2 are equal. Hence, It returns true.

The values in each set are compared with another set for equality.

import 'package:flutter/foundation.dart';

void main() {
  var set1 = {"one", "two", "three"}
  var set2 = {"one", "two", "three"};
  print(setEqual(set1, set2)); // true
}

In the below example, set1 and set2 are different as one of the values (“Two” != “two”) is not equal. Hence, It returns false. So, It compares the exact case match for a string.

import 'package:flutter/foundation.dart';

void main() {
  var set1 = {"one", "Two", "three"}
  var set2 = {"one", "two", "three"};
  print(setEqual(set1, set2)); // fase
}

How to compare two Sets of Objects for equal or in Flutter

this shows you compare two Sets of objects, returns if two Sets are equal, else returns false.

Following are the steps required.

  • Created Employee with id, name, salary, and joinDate
  • Created four Sets in the below example
  • Compared two Sets using
import 'package:flutter/foundation.dart';
import 'dart:convert';

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

void main() {
  final e1 =
      Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
  final e2 =
      Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
  final e3 =
      Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
  final e4 =
      Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));

  final Set<Employee> employees = <Employee>{e1, e2, e3, e4};
  final Set<Employee> employees1 = {e1, e2, e3, e4};
  print(setEquals(employees, employees1)); // true

  final Set<Employee> employees2 = {e1, e3, e4};
  final Set<Employee> employees3 = {e1, e2, e4};
  print(setEquals(employees2, employees3)); // false
}

Conclusion

In this tutorial, Learned how to compare two Set of numbers, strings, and objects equality in Dart and flutter programming