EqualityComparer<T> class
Used in various enumeration methods for allowing user-defined comparisons between complex elements.
EqualityComparer exposes three different forms of comparison - equivalency, ordering, and hashing. Any of the options can be omitted when creating an EqualityComparer instance, in which case the omitted options will revert to default behavior as outlined below.
The EqualityComparer.compare field tests equivalency. Two elements of a
given type are passed to the function, and the function returns whether or
not the elements are deemed equal. If omitted, the function will default to
checking for strict equivalency by using the ==
operator.
(left, right) { return left == right; }
The EqualityComparer.hash field generates hash codes. An element is passed
to the function and the function returns an integer value that represents
the element's hash code. If omitted, the function will default to calling the
object's hashCode
property:
(value) { return value.hashCode; }
The EqualityComparer.sort field tests for sorting order. Two elements of a
given type are passed to the function, and the function returns an integer
that represents the sorting order of the elements. If the left value is lesser
than the right value, the returned integer is negative. If the left value is
greater than the right value, the returned integer is positive. If the two
values are equal, 0 is returned. (This behavior is identical to various
compareTo
methods on Dart types.)
If the EqualityComparer.sort field is omitted, the function's default behavior depends on the type argument passed to EqualityComparer:
int
will default to usingint.compareTo
num
will default to usingnum.compareTo
double
will default to usingdouble.compareTo
String
will default to usingString.compareTo
Duration
will default to usingDuration.compareTo
BigInt
will default to usingBigInt.compareTo
- All other types will default to a non-sorting function:
(left, right) { return 0; }
Constructors
-
EqualityComparer({Comparer<
T> comparer, Hasher<T> hasher, Sorter<T> sorter })
Properties
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
toString(
) → String -
Returns a string representation of this object.
inherited
Operators
-
operator ==(
dynamic other) → bool -
The equality operator. [...]
inherited
Static Methods
-
forType<
T> () → EqualityComparer< T> -
Returns the default EqualityComparer that has been registered for type
T
. [...] -
registerEqualityComparer<
T> (EqualityComparer< T> comparer, { bool overwrite: false }) → bool -
Registers an EqualityComparer object as the default comparer for type
T
, returning abool
stating if a comparer already exists. [...] -
unregisterEqualityComparer<
T> () → EqualityComparer< T> -
Unregisters an EqualityComparer object as the default comparer for type
T
, returning the comparer object that was removed. [...]