aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode" Watch "hashCode" New topic
Author

hashCode

weiquan yuan
Greenhorn

Joined: Jun 13, 2006
Posts: 5
this sentence is always treated right, but it is not complete.

"if x.equals(y) is true, then x.hashCode() == y.hashCode() is true"




should be "if x.equals(y) is true and x, y both implement hashCode() , then x.hashCode() == y.hashCode() is true"

Any Feedback?
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
its true if you are following the equals() hashCode() contract.


however you could write equals() and hashCode() methods that violates this contract where x.equals(y) is true but x.hashCode() != y.hashCode().


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Your implementation of equals does not make sense.

You are comparing the day string using the == operator, which checks the object reference rather than it's logical equality. Use
((ToDos)o).day.equals(this.day); instead .

Also, you did not override hashCode, which means the hashCode implementation of object is in force, which is not what we want.



Implement this and the hashCode() & equals() contract is intact.


JDBCSupport - An easy to use, light-weight JDBC framework -
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
you forgot to override hashcode


SCJP 6
Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
This contract will only comes into play when we are overriding
Equals
Hashcode.

But one question creeps in my mind . Though, I understand the previous statement ,but someway or somehow. There pops up a doubt. As according to the concept of Maps, the hashcode helps to find out the right bucket and then equals find out the right object referencing to it.

Even if we are not overriding the hash method , but internally the without overriden hash method should also adopt this kind of approach, mentioned theoretically.


SCJP 1.6
SCWCD 1.5 (Preparing...)
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Samrat Som wrote:This contract will only comes into play when we are overriding
But one question creeps in my mind . Though, I understand the previous statement ,but someway or somehow. There pops up a doubt. As according to the concept of Maps, the hashcode helps to find out the right bucket and then equals find out the right object referencing to it.

Even if we are not overriding the hash method , but internally the without overriden hash method should also adopt this kind of approach, mentioned theoretically.
If we dont override the hash method then after we store our object in a map the only way we can get it back is using the same object reference. We cannot create a new object wich will be logically equal to our object in a map and find it in the map with this new object, if they equal but dont have the same hashcodes.
Example: you create new little red car as new Car("red") and store it in a set or map, then in some another piece of code, when the reference to the object is long gone (you could even populate your set like carset.add(new Car("red")) and get no reference after that) you eventually whant to know if you have some red car in your collection. So, you do carset.contains(new Car("red")) and if your hashCode() and equals() for Car class are implemented well you will find your little red car in the set. If your hashCode is default Object.hashCode then you have no chance of getting your car from the collection.


[SCJP 6.0]
Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
Thanks Anastasia,

As my understanding goes , we need to override the hashcode function to extract the value ,which we can do it with the help of equals without considering the map data structure.
Map helps for quicker retrieval with the help od hashcode method with the help of equals.
I think hashcode implementation needs to be made mandatory rather than leaving it to decision of the programmer to override it.Otherwise , it beats the basic purpose of Map implementation.

Please add if I am wrong
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
Samrat Som wrote:Thanks Anastasia,

As my understanding goes , we need to override the hashcode function to extract the value ,which we can do it with the help of equals without considering the map data structure.
Map helps for quicker retrieval with the help od hashcode method with the help of equals.
I think hashcode implementation needs to be made mandatory rather than leaving it to decision of the programmer to override it.Otherwise , it beats the basic purpose of Map implementation.

Please add if I am wrong

Basically when you store objects in hashed collection you still can extract values stored there by their references. The only purpose to override hashCode and equals is to extract values stored in the collection using not the references with wich you stored them, but using some other references that refer to objects logically equal to those that store in a map. And this is fully on programmer how he implements his storing/retriving mechanism, whether or not it will be nessesary to retrieve stored values with newly created objects.

Still if you are not intended to deal with hashed collections you dont need hashCode().

Most IDE's will give a warning if you override equals without overriding hashCode(), because they (IDE's) assume that if you override equals then you may be intended to do something with hashed collections, but may be not, so they give you a warning, just in case you care.

Samrat Som
Ranch Hand

Joined: Feb 04, 2009
Posts: 40
Thanks a lot....Anastasia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashCode
 
Similar Threads
HashMap and hashCode()
Hashmap Confusion
Detecting Duplicates in Hashmap
Confusion on Hashcode and equal method
HashMap Duplicate Element Problem