BIS Tech
BIS Tech

Reputation: 19424

how to check two maps are equal in dart

Is it possible to check two maps are equals or not like java equals?

void main() {
  Map map1 = {'size': 38, 'color': 'red'};
  Map map2 = {'size': 38, 'color': 'red'};

  if(map1== map2){//both keys and values
    print('yes');
  }else{
    print('no');
  }
}

Upvotes: 32

Views: 20003

Answers (7)

jamesdlin
jamesdlin

Reputation: 89926

package:quiver provides a mapsEqual function to compare Maps (and similar functions for Lists and Sets). It has no dependence on Flutter and can be used in any Dart project.

Example:

// https://pub.dev/packages/quiver
// "flutter pub add quiver"
// https://pub.dev/documentation/quiver/latest/quiver.collection/quiver.collection-library.html

import 'package:quiver/collection.dart';

// Class Foo does not override its "==" operator.
// This two Foo objects are "equal" according to "=="
// if and only if they are "the same object".

class Foo {
  final int x;

  Foo(this.x);
}

// Class Bar override its "==" operator so two Bar objects
// are "equal" according to "==" if they "are the same value", in
// this case, if the value of their field "x" is the same.
// This is called "having value semantics".

class Bar {
  final int x;

  Bar(this.x);

  @override
  bool operator ==(Object other) => (other is Bar && x == other.x);

  @override
  int get hashCode => x;
}

void testMapIdentityAndEquality() {
  // Integers are "equal" if they "are the same value" ("value semantics")

  final mapInt1 = {'x': 1, 'y': 2, 'z': 3};
  final mapInt2 = {'x': 1, 'y': 2, 'z': 3};

  // Foo objects are "equal" only if they "are the same object".

  final mapFoo1 = {'x': Foo(1), 'y': Foo(2), 'z': Foo(3)};
  final mapFoo2 = {'x': Foo(1), 'y': Foo(2), 'z': Foo(3)};

  // Bar objects are "equal" if they "are the same value" ("value semantics")

  final mapBar1 = {'x': Bar(1), 'y': Bar(2), 'z': Bar(3)};
  final mapBar2 = {'x': Bar(1), 'y': Bar(2), 'z': Bar(3)};

  // Identity and equality of maps holding elements with value semantics.

  assert(identical(mapInt1, mapInt1), "a map is (evidently) IDENTICAL to itself");
  assert(mapInt1 == mapInt1, "a map that is the same object is also EQUAL to itself ('==' defaults to object identity)");

  assert(!identical(mapInt1, mapInt2), "a map is NOT IDENTICAL to another map");
  assert(mapInt1 != mapInt2, "a map is NOT EQUAL to another map, even if the map content is 'the same' and has value semantics");
  assert(mapsEqual(mapInt1, mapInt2), "QUIVER EQUALITY says the maps are EQUAL if map content is 'the same' and has value semantics");

  assert(!identical(mapFoo1, mapFoo2), "a map is NOT IDENTICAL to another map");
  assert(mapFoo1 != mapFoo2, "a map is NOT EQUAL to another map, especially if the map content does not have value semantics");
  assert(!mapsEqual(mapFoo1, mapFoo2), "QUIVER EQUALITY says the maps are NOT EQUAL if the map content 'looks the same' but does not have value semantics");

  assert(!identical(mapBar1, mapBar2), "a map is NOT IDENTICAL to another map");
  assert(mapBar1 != mapBar2, "a map is NOT EQUAL to another map, even if the map content is 'the same' and has value semantics");
  assert(mapsEqual(mapBar1, mapBar2), "QUIVER EQUALITY says the maps are EQUAL if map content is 'the same' and has value semantics"); // what we want

  // More cases

  assert(!mapsEqual(
      <dynamic, dynamic>{'x': Bar(1), 'y': Foo(1)},
      <dynamic, dynamic>{'x': Bar(1),'y': Foo(1)}), 
  "QUIVER EQUALITY says the maps are NOT EQUAL if the map content 'looks the same' but at least some of the elements do not have value semantics");
  assert(!mapsEqual(mapBar1, mapFoo1), "Not even the same type");
}

void main() {
  testMapIdentityAndEquality();
}

Upvotes: 7

Mudassar
Mudassar

Reputation: 241

Use MapEquality().equals(Object a, Object b) from package collection.

import 'package:collection/collection.dart';

MapEquality().equals(map1, map2)

See the description

Upvotes: 24

BIS Tech
BIS Tech

Reputation: 19424

I found mapEquals.

    import 'package:flutter/foundation.dart';
    
    void main() {
      Map map1 = {'size': 38, 'color': 'red'};
      Map map2 = {'size': 38, 'color': 'red'};
      
    
      if(mapEquals(map1, map2)){
        print('yes');
      }else{
        print('no');
      }
    }

The mapEquals function is a utility function from the 'package:flutter/foundation.dart' package in Flutter. It is used to compare two maps for equality. This function returns true if both maps have the same keys and their corresponding values are also equal, otherwise, it returns false.

In the given code snippet, you have two maps: map1 and map2. Both maps have the same keys ('size' and 'color') and their corresponding values (38 and 'red') are also equal.

The mapEquals(map1, map2) function is called to compare these maps. Since both maps are equal, the function returns true and 'yes' will be printed as the output. If the maps were not equal, 'no' would have been printed.

Upvotes: 41

Hesam
Hesam

Reputation: 53600

I tried to not use any other dependencies. You can copy/paste the following code in Dart Playground and test it.

void main() {
  final map1 = {'a':1, 'b':3};
  final map2 = {'a':1, 'b':3};
  final map3 = {'b':3, 'a':1};
  final map4 = {'b':3, 'a':1, 'c':1};
  bool result = _mapEquals(map1, map2);
  print(result); // true
  result = _mapEquals(map1, map3);
  print(result); // true
  result = _mapEquals(map1, map4);
  print(result); // false
}

bool _mapEquals(Map<String, int> map1, Map<String, int> map2) {
  if (map1.keys.length != map2.keys.length) return false;

  for (String k in map1.keys) {
    if (!map2.containsKey(k)) return false;
    if (map1[k] != map2[k]) return false;
  }

  return true;
}

Upvotes: 0

Ahmed Magdy Mohammad
Ahmed Magdy Mohammad

Reputation: 25

    Map v1 = {'name': "ahmed", "age": 24};
    Map v2 = {'name': "ahmed", "age": 24};
    if(v1.toString() == v2.toString())
  print('yes');
    else{
      print('no');
    }
  }

Upvotes: 0

Abhishek Das
Abhishek Das

Reputation: 361

For Flutter, if you have a nested Map and you need to check its equality with another nested object in terms of key and value mappings use :

import 'package:collection/collection.dart';

if(DeepCollectionEquality().equals(map1, map2)) {
  print('Maps are equal');
} else {
  print('Maps are not equal');
}

Upvotes: 36

Avinash Yalgonde
Avinash Yalgonde

Reputation: 111

Any of these answer didn't worked for me.

I tried map1.toString() == map2.toString() and it worked.

Upvotes: 1

Related Questions