• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding the HashCodeBuilder api in Apache .

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic