Cheers!
RSR
Originally posted by rakesh sugirtharaj:
I can understand why hashcode has to be overrided each time equals is changed but where do these find their application other than Sets? :roll:
apigee, a better way to API!
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
What do you mean, 'only String gets to have overloaded operators'?
"I'm not back." - Bill Harding, Twister
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
Compatibilty between compareTo() and equals()/hashCode() may be nice, but it is never a requirement in order to get consistent, sensible results from a sort.
Originally posted by Jim Yingst:
Do you mean java.util.Arrays? I don't think that's correct. Methods like sort() and binarySearch() rely on the Comparable and Comparator interfaces to determine both order and equality. There's no need for hashCode() or equals(). Meanwhile Arrays.equals() relies on the equals() method of each object in the array, but not hashCode(). Obviously, Arrays.hashCode() does rely on the hashCode() of each contained object - but aside from that, java.util.Arrays() does not use hashCode().
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Originally posted by Rob Prime:
As specified by Set, the contains method uses equals to check if an object is present. SortedSet does not override this method so this should still hold. However, TreeSet uses (through TreeMap) compareTo (or its Comparator's compare) instead of equals.
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
"I'm not back." - Bill Harding, Twister
Jim Yingst:
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface.
Jim Yingst:
This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal.
The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
"I'm not back." - Bill Harding, Twister
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
But it's not a violation unless the compareTo() or comare() method used is incompatible with equals().
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
Not really. I don't see how this would lead to losing objects. It's just that the equals() method isn't what is used to define equality, when using a SortedSet (or SortedMap). Objects in the set or map may still be found and retrieved - unlike what happens in a HashSet if the hashCode() is incompatible with equals().
Originally posted by Jim Yingst:
But if you implement compareTo() or compares() to be compatible with equals(), that requirement is not violated.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|