• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

how hasing works in java

 
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May i know how hashing works in java?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean hashing as in: MD5/SHA, as in hashCode(), or as in HashMap/Set()?
 
Marshal
Posts: 72904
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 12988
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 72904
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic