Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31641 > unrolled thread

what are the senarios when we should override equals and hashcode

Started byAjay Paul <ajaykpaul@gmail.com>
First post2015-05-02 17:39 -0700
Last post2015-05-12 14:17 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  what are the senarios when we should override equals and hashcode Ajay Paul <ajaykpaul@gmail.com> - 2015-05-02 17:39 -0700
    Re: what are the senarios when we should override equals and hashcode Arne Vajhøj <arne@vajhoej.dk> - 2015-05-03 10:50 -0400
    Re: what are the senarios when we should override equals and hashcode Lew <lewbloch@gmail.com> - 2015-05-12 14:17 -0700

#31641 — what are the senarios when we should override equals and hashcode

FromAjay Paul <ajaykpaul@gmail.com>
Date2015-05-02 17:39 -0700
Subjectwhat are the senarios when we should override equals and hashcode
Message-ID<d870a503-377a-4ca7-bdf8-e5c495af244e@googlegroups.com>
What are the senarios when we should override equals and when we don't have to the same.

[toc] | [next] | [standalone]


#31644

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-05-03 10:50 -0400
Message-ID<554635c4$0$281$14726298@news.sunsite.dk>
In reply to#31641
On 5/2/2015 8:39 PM, Ajay Paul wrote:
> What are the senarios when we should override equals and when we don't have to the same.

You should override them when you need to them to overridden.

:-)

A typical case would be data classes where you need to compare
instances either explicit in your code or implicit by stuffing
them into collections and let the collection code do the comparisons.

Arne

[toc] | [prev] | [next] | [standalone]


#31686

FromLew <lewbloch@gmail.com>
Date2015-05-12 14:17 -0700
Message-ID<41d29704-1adb-43d6-861e-33cf9c70d46e@googlegroups.com>
In reply to#31641
On Saturday, May 2, 2015 at 5:40:01 PM UTC-7, Ajay Paul wrote:
> What are the senarios when we should override equals and when we don't have to the same.

If you compare objects by identity, i.e., "Equality means it is the exact same object", then you don't override 'equals()', 'hashCode()' (not "hashcode()"), 'toString()', or 'compareTo()'. If you compare objects by state, then you must override 'equals()' and 'hashCode()', you should override 'toString()', and if you implement 'Comparable', 'compareTo()'. All four methods (where all four exist) should be consistent with each other.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web