when to implement comparable and when to implement equals in Java -
in java, when should implement comparable<something>
versus implementing equals
method? understand every time implement equals
have implement hash code
.
edit
based on answers getting below:
is safe if implement comparable
don't need implement equals
, hashcode
? in: whatever can accomplish equal
included in compareto
? example, want able compare 2 bsts equality. implementing hashcode
seems daunting; comparable
sufficient?
if ever need compare them equality (or put them in hashmap
or hashset
same) need implement equals
, hashcode
.
if objects have implicit order , indend sort them (or put them in treemap
or treeset
sorting) must implement comparable
or provide comparator
.
Comments
Post a Comment