No, it doesn’t. It says that if you override equals you must override hashCode too. That is the other way round. It is probably a good idea to override both together all the time, but the contract permits you to override hash and not override equals.divya chowdhary wrote: . . . But a contract in java says whenever you override hashCode method you must override equals method too. . . .
divya chowdhary wrote:So hashCode method is helpful in storing the objects where as equals method helps in retrieving objects from the collection. Am i right?
divya chowdhary wrote:But a contract in java says whenever you override hashCode method you must override equals method too.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:
No, it doesn’t. It says that if you override equals you must override hashCode too. That is the other way round. It is probably a good idea to override both together all the time, but the contract permits you to override hash and not override equals.divya chowdhary wrote: . . . But a contract in java says whenever you override hashCode method you must override equals method too. . . .
Which is totally different from what I said earlier.divya chowdhary wrote: . . .
In the java docs put method was defined as’t over
"Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced."
No, it isn’t. That is exactly how you have designed your class. Put all my instances as keys in the same bucket but we all get different values. It is behaving exactly as the documentation says. Because the equals method returns false, the put method interprets that as two different keys.
I have developed a sample program to check this functionality. here i have just override hashCode method but not equals. So it considers the default equals method implementation and stores the object. As a result two objects with same key is stored which is a meaningless.
The map class behaved exactly as the documentation said in all instances. When you don’t override the equals() method, but do override hash, the map sees them as looking the same, but not being the same. Like twins. The map sees them as different objects. So does equals(). If you don’t override it, equals uses return other == this; or similar. If you override both, then they cease to be twins. They come along to the map saying, “We are not twins, we don’t simply look the same, we are both the same.” If you wrote the class, then you define what “same” means, in the equals method.But when i override both methods the statement what was said in java docs getting true. . . .
Also why it is saying that c1.equals(c3) is false even though the hashcode is same for both objects when i won't override equals. . . .
divya chowdhary wrote:In my last example both c1, c3 objects have same hash code values but why is it showing the result "false" when comparing "c1==c3", as the "==" returns true when both values are same?
~ Mansukh
Divya wrote: Finally what is the big mistake in not overriding hashCode method when we override equals method? Is the only reason behind this is the bad design of storing equal objects in different buckets or any performance issues
~ Mansukh
Campbell Ritchie wrote:That is not a good example; if Integer objects are cached up to 0x0400 that program will behave differently from the minimum size of cache.
~ Mansukh
~ Mansukh
Campbell Ritchie wrote:Apart from the fact that your equals() method is written incorrectly?
~ Mansukh
Paul Clapham wrote:You made the common beginner's mistake of using == to compare two Strings for equality of contents instead of the equals() method.
~ Mansukh
Mansukhdeep Thind wrote:
OK Now Paul? Or is there something still missing?
Mansukhdeep Thind wrote:OK Now Paul? Or is there something still missing?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
OK Now Paul? Or is there something still missing?
You made it worse.
You still have the error of using == to compare objects where you should use equals(), and you broke and then removed an optimization at the beginning of the method.
Changing that == to equals() was a mistake, the discovery of which is what I assume led you to comment it out. You should have left it as-is. That's one case where you do want to use ==.
Campbell wrote: That version has the advantage that you are not using multiple return. It still has the vulnerability that it will suffer an Exception if either of the fields in the obj object points to null.
~ Mansukh
Mansukhdeep Thind wrote:
Campbell Ritchie wrote:You do realise that using instanceof can cause the method to lose symmetry if the class is subclassed? Winston G’s technique of marking everything final will preserve symmetry,
~ Mansukh
Mansukhdeep Thind wrote:
Campbell Ritchie wrote:You do realise that using instanceof can cause the method to lose symmetry if the class is subclassed? Winston G’s technique of marking everything final will preserve symmetry,
What do you mean when you say that using instanceof can cause the method to lose symmetry if I have a child class that extends Cars? what is meant by method symmetry?
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
That null test is pointless. If it's null, instanceof will evaluate to false.
Jeff Verdegan wrote:
This is still subject to throwing NPE if name or color are allowed to be null.
~ Mansukh
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Campbell Ritchie wrote:You do realise that using instanceof can cause the method to lose symmetry if the class is subclassed? Winston G’s technique of marking everything final will preserve symmetry,
What do you mean when you say that using instanceof can cause the method to lose symmetry if I have a child class that extends Cars? what is meant by method symmetry?
As explained in the javadocs for equals(), x.equals(y) must return the same value as y.equals(x) for all non-null x and y.
If you create a class SportsCar extends Car, and SportsCar's equals() method tests for instanceof SportsCar, then Car.equals(SportsCar) can return true, but SportsCar.equals(Car) can only return false. Therefore, symmetry can be violated.
~ Mansukh
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:
This is still subject to throwing NPE if name or color are allowed to be null.
How to overcome this? Do I put a null check in the constructor before initializing them
or throw a NPE if any of these is a null value inside the equals method?
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:
If you create a class SportsCar extends Car, and SportsCar's equals() method tests for instanceof SportsCar, then Car.equals(SportsCar) can return true, but SportsCar.equals(Car) can only return false. Therefore, symmetry can be violated.
Is marking it as final the only way to prevent that problem? Or is there another way too?
~ Mansukh
Mansukhdeep Thind wrote:I think I shall stick to Winston's advice for now.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:I think I shall stick to Winston's advice for now.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
Mansukhdeep Thind wrote:I don't think Divya is coming back with any more of equals() / hashCode() doubts when she reads all this...let's see if she has understood the importance of equal objects should have equal hash code rule which is why I wrote the code in the first place.
![]()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Consider Paul's rocket mass heater. |