This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes how hasing works in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how hasing works in java" Watch "how hasing works in java" New topic
Author

how hasing works in java

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

May i know how hashing works in java?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Do you mean hashing as in: MD5/SHA, as in hashCode(), or as in HashMap/Set()?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Sep 20, 2010
Posts: 3647
    
  17

And for hash tables you can usually find an explanation in any computer algorithms and data-types book.
Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Mar 17, 2011
Posts: 8043
    
  22

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.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
wood burning stoves
 
subject: how hasing works in java