I want to make a class usable in SortedSet
| SortedMap
.
class MyClass implements Comparable<MyClass>{
// the only thing relevant to comparisons:
private final String name;
//...
}
The class' instances must be sorted by their name property.
However, I don't want equally named instances to be considered as equal.
So a SortedSet
content would look like a, a, a, b, c.
(Normally, SortedSet
would only allow a, b, c)
First of all: is this (philosophically) consistent?
If so, do I have to expect unpredictable behavior, when I don't
override equals(...)
and hashCode()
?
Edit:
I am sorry, my question seems inconsistent:
I want to put multiple "equal" values inside a set, which doesn't allow this
by concept.
So, please don't reply to my question anymore.
Thanks to all who already replied.
But if compareTo() is "inconsistent with equals" then this code can throw the exception, because a. compareTo(b) can return zero when a. equals(b) is false.
Equals can be more efficient then compareTo. If the length of the character sequences in String doesn't match there is no way the Strings are equal so rejection can be much faster. Moreover if it is same object (identity equality rather then logical equality), it will also be more efficient.
The equals() tells the equality of two strings whereas the compareTo() method tell how strings are compared lexicographically.
It is recommended that compareTo only returns 0 , if a call to equals on the same objects would return true : The natural ordering for a class C is said to be consistent with equals if and only if e1. compareTo(e2) == 0 has the same boolean value as e1. equals(e2) for every e1 and e2 of class C.
Let me ask you a question: does it make sense to have a.compareTo(b)
return 0 and a.equals(b)
return false
?
I would use a Comparator<MyClass>
instead. This is why all SortedMap
/SortedSet
implementations that I know of allow you to pass in a Comparator
at creation.
From the Javadoc for Comparable
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals
If you want to have compareTo inconsistent with equals(), it is recommended that you instead use an explicit comparator by providing a class that implements Comparator.
If so, do I have to expect unpredictable behavior, when I don't override equals(...) and hashcode()?
You should still override equals() and hashcode(). Whether or not equals() and hashcode() are consistent with compareTo is a different matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With