• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you forgot to override hashcode
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Samrat Som
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot....Anastasia
 
reply
    Bookmark Topic Watch Topic
  • New Topic