What is the Java equivalent of .NET's IEquatable<T> Interface?
What is the Java equiv开发者_开发知识库alent of .NET's IEquatable Interface?
Unfortunately I don't believe there is one - which is a pain in terms of providing hash maps etc with custom equality comparisons :(
Obviously there's Comparable<T>
as an equivalent to IComparable<T>
and Comparator<T>
for IComparer<T>
, but I don't believe there's any equivalent of IEqualityComparer<T>
and IEquatable<T>
.
There may be third party libraries providing the same sort of interface and maps which use them of course...
If you are just looking for that interface to implement the equals()
method, you can override the object.equals()
method directly.
The IEquatable<T>
interface is primarily useful with generic classes that store many instances of what may be unboxed structure types; it's marginally useful with sealed types, and worse than useless with unsealed types. There's thus really wouldn't be any point to implementing IEquatable<T>
in Java.
There's an equivalent type in Guava called Equivalence<T>.
精彩评论