~ Mansukh
Campbell Ritchie wrote:Another place to look is Angelika Langer Java hash code equals. Beware: some of it is in German.
~ Mansukh
Joanne
Junilu Lacar wrote:@OP:
That's not an explanation I would accept if I were interviewing you.
You override equals() when "equality" of objects means more than the default "equals()" which basically says if the other object is not this object, equals is false. There are other considerations, especially when factoring in inheritance.
You override hashcode() when you override equals() so that collections and other classes that use the hash of your object for efficient operation and depend on the contract between equals() and hashcode() to be honored, will work correctly with your object.
~ Mansukh
Mansukhdeep Thind wrote:OK Junilu. Now I am confused. Could you perhaps elaborate with a simple example ? May be provide a simple use case where you override equals() and hashCode(). Also explain why you did what you did. I need to get this into my head crystal clear.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:@OP:
That's not an explanation I would accept if I were interviewing you.
You override equals() when "equality" of objects means more than the default "equals()" which basically says if the other object is not this object, equals is false. There are other considerations, especially when factoring in inheritance.
You override hashcode() when you override equals() so that collections and other classes that use the hash of your object for efficient operation and depend on the contract between equals() and hashcode() to be honored, will work correctly with your object.
~ Mansukh
Mansukhdeep Thind wrote:We override hashCode() and equals() methods of Object class when we want to ensure that Hashing Collection classes(like Hashtable, HashMap, HashSet etc..) behave predictably and efficiently when we add/search for our stored objects in them.
Mansukhdeep Thind wrote:We need to correctly override equals in order to make sure that we are searching for the correct type of object and then compare the instance variable values of these.
Mansukhdeep Thind wrote:Why override hashCode() when overriding equals?
Mansukhdeep Thind wrote:which are in a manner of speaking identified by hash codes which in turn are some transformation value of the actual memory addresses of objects.
Mansukhdeep Thind wrote:If we do not override hashCode() method of Object class, then surely the 2 objects in question will have different hash codes, although their equals() test returns true.
Joanne
~ Mansukh
Then you can say what those special cases are.Always override equals and hashCode, except for a few special cases.
Campbell Ritchie wrote:Disagree about those answers. The correct answer is, in my opinion,
Then you can say what those special cases are.Always override equals and hashCode, except for a few special cases.
~ Mansukh
Campbell Ritchie wrote:Everybody’s.
You should not go round thinking, “do I have to override equals() and hashCode() here?”. You think you must override them unless your class falls in one of the special cases where they are not required.
~ Mansukh
Campbell Ritchie wrote:Everybody’s.
You should not go round thinking, “do I have to override equals() and hashCode() here?”. You think you must override them unless your class falls in one of the special cases where they are not required.
Campbell Ritchie wrote:Classes which don’t encapsulate any values don’t need equals methods, because they have nothing to compare.
~ Mansukh
Campbell Ritchie wrote:Yes, I think I have gone too far the other way.
DAOs and bridges obviously don’t need overridden equals methods.
Nor do classes designed to change their state very quickly.
Nor do classes intended as short-lived throwaway objects, because they are never around long enough to need equality testing.
Nor do utility classes, because you never instantiate them and can’t call equals.
Nor do classes which are singletons (which to all intents and purposes means enum elements), because the java.lang.Object version does exactly what you want.
Nor do things like Class and Thread because every instance is implicitly different from every other instance.
Most classes shown on these fora do not have overridden equals methods because they are short-lived classes used for training.
~ Mansukh
Junilu Lacar wrote:Moreso with classes like controllers, view-models, facades, DAOs, builders, factories, bridges, adapters, decorators, etc. There are so many kinds of classes that you don't really care about their "logical equality". Why spend the time and effort writing equals() and hasCode() for these when you're never going to need them?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I think data access objects and don’t know.Mansukhdeep Thind wrote: . . . What are DAOs and bridges?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |