How can you determine whether two objects are equal or not? Basically, this is defined by objects (refer to the How it works… section of this recipe). Obviously, two equal objects will be of the same class (so the same type) and have the same value(s).
In the comparing_objects
program, we define a class Person
, override ==
and hashcode
, and test the equality of some objects, as shown in the following code:
void main() { var p1 = new Person("Jane Wilkins", "485-56-7861", DateTime.parse("1973-05-08")); var p2 = new Person("Barack Obama", "432-94-1282", DateTime.parse("1961-08-04")); var p3 = p1; var p4 = new Person("Jane Wilkins", "485-56-7861", DateTime.parse("1973-05-08")); // with == and hashCode from Object: // (comment out == and hashCode in class Person) print(p2==p1); //false: p1 and p2 are different print(p3==p1); //true: p3 and p1 are the same object print(p4==p1); //false: p4 and p1 are different objects print(identical(p1, p3)); //true print(identical(p1, p4)); //false print(p1.hashCode); // 998736967 print(p2.hashCode); // 676682609 print(p3.hashCode); // 998736967 // with specific == and hashCode for class Person: print(p2==p1); //false: p1 and p2 are different print(p3==p1); //true: p3 and p1 are the same object print(p4==p1); //true: p4 and p1 are the same Person print(identical(p1, p3)); //true print(identical(p1, p4)); //false print(p1.hashCode); // 105660000000 print(p2.hashCode); // -265428000000 print(p3.hashCode); // 105660000000 } class Person { String name; String ssn; // social security number DateTime birthdate; Person(this.name, this.ssn, this.birthdate); toString() => 'I am $name, born on $birthdate'; operator ==(Person other) => this.ssn == other.ssn; int get hashCode => birthdate.millisecondsSinceEpoch; }
The Object
superclass has a bool ==(other)
method that defines the equality of objects. This returns
true
only when this
and other
are the same object, that is, they reference the same object in the (heap) memory. This is the default behavior, unless you override it in your classes. The !=
operator is the negation of this. Another way to test this is the top-level identical function from dart:core bool identical(Object a, Object b)
.
Every object has an integer hashcode
returned by the getter int ge
t hashCode
. Two objects that are equal have the same hashcode, but this can differ between two runs of a program. Any subclass can have its own version to define equality between its objects.
If you want to use instances of a class as keys in maps, then you have to overload the ==
operator and the hashCode
method.