Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how hasing works in java

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May i know how hashing works in java?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean hashing as in: MD5/SHA, as in hashCode(), or as in HashMap/Set()?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to provide your own algorithms, and you should find details of SHA etc in any algorithms book. You can find a recommended hashCode method in Joshua Bloch's Effective Java™ or Bruce Eckel's Thinking in Java™ (TiJ); Eckel acknowledges Bloch there. You can find the 3rd edition of TiJ or a sample chapter of Bloch (old edition: try here) free of charge if you search the net. There are subtle differences in Bloch's recommendations in the 2nd edition.
 
Stephan van Hulst
Bartender
Posts: 6323
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for hash tables you can usually find an explanation in any computer algorithms and data-types book.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You would have to provide your own algorithms, and you should find details of SHA etc in any algorithms book. You can find a recommended hashCode method in Joshua Bloch's Effective Java™ or Bruce Eckel's Thinking in Java™ (TiJ); Eckel acknowledges Bloch there. You can find the 3rd edition of TiJ or a sample chapter of Bloch (old edition: try here) free of charge if you search the net. There are subtle differences in Bloch's recommendations in the 2nd edition.


My understanding is that from the effective java link mentioned above that, hashing checks if equals() and hashcode() contract is met or not. if met hashing is done otherwise hashing fails?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't remember that; are you sure you have read the link correctly?

As far as I remember, it explains how you ensure equals() fulfils its general contract, and how to write a hashCode method which also maintains that general contract, but hashing cannot fail for the algorithms shown there.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rauhl Roy wrote:My understanding is that from the effective java link mentioned above that, hashing checks if equals() and hashcode() contract is met or not. if met hashing is done otherwise hashing fails?

I don't think so; the only things I've ever read are:
1. If you implement equals(), you should also implement hashCode().
2. If object A is equal() to object B then they must return the same hashcode.
(That is NOT the same as saying that two objects, A and B, that return the same hashcode must be equal() - in fact, such a guarantee is pretty well impossible).
3. It generally helps for hashed collections if, as far as possible, objects that are NOT equal() produce different hashcodes.

Winston

BTW - You may be interested to know that HashSet and HashMap actually "re-hash" your hash to help prevent bucket collisions, so you don't even need to worry too much about "hash spread"; just make sure that they're likely to be distinct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic