{

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.

Compare set numbers strings and objects for equality  in Dart or Flutter

Two sets are equal if it matches below criteria

  • Length of two sets are equal
  • Order of elements is not important
  • If element is an object, The key and values of an objects in both sets should 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 equal comparison result with boolean type.

bool setEquals<T>(Set<T>? a, Set<T>? b)
  • true: Returns true if two Set are equal with ignoring the order of elements.
  • false: Elements in the Set. Here, Compare the two Set of numbers and returns true if 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 Set of strings in a flutter

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

The values in each set are compared with other 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 Set of objects, returns if two Set 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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example