• 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

HashMap/HashBucket

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the hashmap class determine which buckets to put keys in? Is each value that is taken in just placed in a new bucket. Ex:

bucket 1 : key 1
bucket 2 : key 2
bucket 3 : key 3
bucket 4: key 4

bucket 1: key 5
bucket 2: key 6
etc.....

I know the default size is 16 and that the second constructor will override the default size for hashmap.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally it computes the hash code of the object, then applies some function which maps that number into a bucket number. The most obvious such function is "% numberOfBuckets" but any function which maps the integers evenly into a small subset of the integers would do.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Generally it computes the hash code of the object, then applies some function which maps that number into a bucket number. The most obvious such function is "% numberOfBuckets" but any function which maps the integers evenly into a small subset of the integers would do.



Is their a way to override the function to control the mapping of the buckets?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:Is their a way to override the function to control the mapping of the buckets?



I don't think so, without rewriting the class. But why would you need to? As long as the objects have a suitable hashCode() method the built-in mechanism will work well.

Note that HashMaps will work if two objects get put in the same bucket - it's just more efficient if that doesn't happen very often.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Sexton wrote:Is their a way to override the function to control the mapping of the buckets?


No (at least not with HashMap) and, as Paul says, why would you want to? The whole point of a "hashed" collection is that an object's location is directly related to its hash, which is why they're so fast. Indeed the reason why 16 is the default, and internal capacity is increased by powers of two is that (theoretically) the hashcode only needs to be masked in order to render a bucket index (although HashMap actually does a bit of other "bit mangling" stuff as well); and masking operations are blisteringly fast - typically only one or two machine instructions.

However, if it's element ordering that interests you, LinkedHashSet preserves elements in insertion order, and LinkedHashMap can maintain keys in other orders too - the most common one being "last used".

HIH

Winston
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies......
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic