Sub topic 12
Equality and `hashCode`: Custom Objects in Sets and Map Keys
Assume you know Dart classes, generics, null safety, and basic collection operations. This chapter explains how Dart decides whether two objects are equal when they appear in a Set or act as keys in a Map.
Identity versus value equality
By default, instances of a class use identity equality. Two objects with the same field values are still different objects if they were created separately.
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
void main() {
print(Point(1, 2) == Point(1, 2)); // false
}The == operator compares the object identities because Point has not defined its own equality rule. This differs from primitive values such as integers and strings, which already compare by value.
Value equality means that two objects are equal when the data that identifies them is equal. For a point, that data might be its x and y coordinates.
Implementing `==` and `hashCode`
When you override ==, you must also override hashCode. A set or map uses the hash code to find a likely location quickly, then uses == to confirm equality.
class Point {
final int x;
final int y;
const Point(this.x, this.y);
@override
bool operator ==(Object other) =>
other is Point && other.x == x && other.y == y;
@override
int get hashCode => Object.hash(x, y);
}
void main() {
final first = Point(1, 2);
final second = Point(1, 2);
print(first == second); // true
final points = {first, second};
print(points.length); // 1
final names = <Point, String>{
const Point(1, 2): 'origin-nearby',
};
print(names[const Point(1, 2)]); // origin-nearby
}The equality method first checks that other is a Point. It then compares both identifying fields. Object.hash(x, y) produces a combined hash from those same fields.
The essential contract is:
| Rule | Meaning |
|---|---|
| Equal objects must have equal hash codes | If `a == b` is true, `a.hashCode == b.hashCode` must also be true. |
| Equal hash codes do not require equality | Different objects can have a hash collision. Dart then checks `==`. |
Use exactly the fields that participate in equality when calculating the hash code. If x and y define equality, both must contribute to hashCode.
Common errors and mutable keys
Do not use mutable fields for equality if an object will be stored in a set or used as a map key. Changing such a field after insertion changes the object's hash code. The collection may then search in the old location and fail to find an object that is still present.
Prefer immutable key objects:
- Declare identity fields with
final. - Avoid changing fields used by
==andhashCode. - Include the same fields in both implementations.
- Check the other object's type before reading its fields.
A list does not automatically compare its contents by value. For example, [1, 2] == [1, 2] is normally false because the lists are separate objects. If you need content-based comparison for larger structures, use an appropriate comparison utility or define a value object.
Exercise
What can go wrong if a map key changes a field used by its == and hashCode implementations after insertion?
Show answer
Its hash code can change, so the map may search in a different location and fail to find the existing entry.