Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Java equivalent of .NET's IEquatable<T> Interface?

Tags:

java

.net

What is the Java equivalent of .NET's IEquatable Interface?

like image 418
Jonathan Allen Avatar asked Jun 10 '10 06:06

Jonathan Allen


4 Answers

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...

like image 87
Jon Skeet Avatar answered Sep 30 '22 17:09

Jon Skeet


If you are just looking for that interface to implement the equals() method, you can override the object.equals() method directly.

like image 20
Ahmed Mostafa Abdel-Baky Avatar answered Sep 30 '22 19:09

Ahmed Mostafa Abdel-Baky


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.

like image 45
supercat Avatar answered Sep 30 '22 17:09

supercat


There's an equivalent type in Guava called Equivalence<T>.

like image 31
Craig Gidney Avatar answered Sep 30 '22 17:09

Craig Gidney