File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36453
    
  15
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: 3361
    
    9
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: 36453
    
  15
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: 7015
    
  16

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?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how hasing works in java
 
Similar Threads
With JavaServerFaces
java help
How to make the reference object also null when the method makes its parameter null value?
link from menubar to the selected page
Hashing