wood burning stoves 2.0*
The moose likes Java in General and the fly likes Regarding the HashCodeBuilder api in Apache . 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 "Regarding the HashCodeBuilder api in Apache ." Watch "Regarding the HashCodeBuilder api in Apache ." New topic
Author

Regarding the HashCodeBuilder api in Apache .

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi all.


I am using org.apache.commons.lang.builder.HashCodeBuilder to generate the hashcode of an object . Can anyone explain where to use the HashCodeBuilder() and where HashCodeBuilder(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber) has to be used . I read the docs but couldnt understand . Can anyone elaborate .

Thanks,
Ragi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You would apply a SomethingBuilder to the class itself; what it does is take all the fields and works out their hash codes. Then it takes a starting value (initialNonZero...) and in turn adds those hash codes to it. After each addition it multiplies the total by the other non-zero number. Then it turns that operation into Java™ code and inserts it into the class as a hashCode method.
You use it when writing the code. You can get similar functionality from IDEs, where you have an option to "insert equals/hashCode method from fields".
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Thank you for your prompt reply but what i am asking is the advantage of one constructor over the other ie the HashCodeBuilder() and the
HashCodeBuilder(int startValue, int multiplier). Can you please explain ?


Thanks
Ragi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
It allows you to specify the multiplication factor.You can start off with a different value from 0 for hash and a different value from 0x1f (31) for multiplier.

That makes a really really big difference, particularly if you know all about has algorithms and can think of a better algorithm . . . and if you believe that, you'll believe anything.

They think there are people who want the option of choosing their own multiplier and starting value. Remember this is a library class. When you design ordinary classes, you want to give them the smallest public interface yo ucan get away with. When you design library classes, you tend to put things in because it is possible somebody would want them. So library classes tend to ahve big public interfaces.
 
jQuery in Action, 2nd edition
 
subject: Regarding the HashCodeBuilder api in Apache .