Paul Clapham wrote:Well, there's a copy-paste-forget-to-edit error at line 14, but apart from that it returns false if any of the three fields are different between the two objects. Is that not what you wanted?
Saurabh Pillai wrote:
Paul Clapham wrote:Well, there's a copy-paste-forget-to-edit error at line 14, but apart from that it returns false if any of the three fields are different between the two objects. Is that not what you wanted?
Yes, I forgot to edit fully. Sorry about that. Now I have fixed it.
Consider this,
1) field1 = 1, field2 = 2, field3=3 It would be added to Set.
2) field1 = 1, field2=2, field3=2 This record would be discarded according for IDE generated equals() method because field1 id duplicate.
Saurabh Pillai wrote:This is what I have from IDE generated method (project specific information renamed) . I want uniqueness in combination of all 3 fields.
Campbell Ritchie wrote:I wouldn’t write an equals() method with all those return statements myself.
Saurabh Pillai wrote:I can modify the IDE generated equals method according to my need (without ending the world), right? Just checking :-)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Saurabh Pillai wrote:I guess code should be modified to something like,...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Saurabh Pillai wrote:I can modify the IDE generated equals method according to my need (without ending the world), right? Just checking :-)
Yes. In fact I'd recommend it, because that code is
(a) awful, and
Unfortunately, there's a lot to know about equals(), and a lot of ways to write them (right and wrong).
The line your IDE has taken is to use an actual class comparison as part of the "equality decision", which I really don't like (and I expect disagreement from my colleagues here), even though it is generally regarded as the "safest" route.
It's fast (quite a bit faster than using a reflective method like getClass());
The only other tip I can give you is to use equals() for all reference field checks that are part of your method, and '==' for all primitives; and forget all that null nonsense.
Jeff Verdegan wrote:Can you back that up? I'd expect getClass() to be faster than, or at least as fast as, instanceof...
Really, instanceof is no less reflective than getClass(), and may be more so.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Saurabh Pillai wrote:I can modify the IDE generated equals method according to my need (without ending the world), right? Just checking :-)
Yes. In fact I'd recommend it, because that code is
(a) awful
This is a very good point which I concede.Furthermore, it will work for any subclass that doesn't require any additional value checks - and it's there that you usually run into problems. I leave it to you to read all about them.
Dennis Deems wrote:
Winston Gutkowski wrote:
Saurabh Pillai wrote:I can modify the IDE generated equals method according to my need (without ending the world), right? Just checking :-)
Yes. In fact I'd recommend it, because that code is
(a) awful
It's not awful. You can glance at it and tell at every step what decision is being made and what the outcome will be.
Eclipse generates an implementation that is very like what is recommended by Josh Bloch.
Of course, if your goal is to learn by getting your hands dirty implementing equals and hashCode, then you shouldn't be using a generated method in the first place.
Both, at different times.Jeff Verdegan wrote: . . . Do you use a "result" variable? Chained ternary operators? . . .
Dennis Deems wrote:It's not awful. You can glance at it and tell at every step what decision is being made and what the outcome will be. Eclipse generates an implementation that is very like what is recommended by Josh Bloch.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paul Clapham wrote:The only problem was that you were thinking of the rules for adding an element to a Set, instead of just thinking about how to tell if two objects were equal and you got yourself all mixed up.
Saurabh Pillai wrote:
Also, I prefer this,
written as wherever it reduces the confusion for me.
Don't get me started about those stupid light bulbs. |