• 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

load factor

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain what is initial capacity and loading factor? I studied the docs but i dont understand it clearly..
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rakesh sugirtharaj:
Can anyone explain what is initial capacity and loading factor? I studied the docs but i dont understand it clearly..



Initial Capacity is about the size and Load factor determines at which capacity resize takes place.

One Example:
LinkedHashMap instance has a default capacity (16) and load factor (0.75).
[ December 26, 2007: Message edited by: Balasubramanian Chandrasekaran ]
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By size you mean the individual bucket size or the entire Set's size?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Load factor of a Map, not a Set; Sets don't have load factors.

The size means how many entries there are in the Map, not the size of buckets. A Map with
  • 2 buckets with 3 entries each, and
  • 2 buckets with 1 entry each, and
  • the default settings for HashMap (load factor = 0.75, capacity I think = 16)
  • will have
  • 8 entries
  • Occupied capacity = 0.5
  • 12 empty buckets
  • Now, if you add 4 entries you get to 12 entries, and still at least 8 empty buckets, depending on which buckets the entries went into. You now have occupied capacity = 0.75, same as the load factor, so it will recalculate all the hashcodes, and create more buckets. It says the capacity is approximately doubled in the HashMap API, so you get
  • capacity = 32
  • load factor = 0.75 (no change)
  • occupied capacity = 0.375
  • size = 12 (as before)
  • empty buckets = anything between 20 and 31.
  • I hope that makes it clearer. Read the API documentation for HashMap for more details.
     
    rakesh sugirtharaj
    Ranch Hand
    Posts: 151
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks bala,ritchie!
     
    Wanderer
    Posts: 18671
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    [Campbell]: Load factor of a Map, not a Set; Sets don't have load factors.

    HashMap, Hashtable and HashSet all have load factors and initial capacities. Other implementations like TreeMap and TreeSet do not.
     
    Sheriff
    Posts: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Seeing as HashSet uses a HashMap internally for its storage, it would have really surprised me if HashSet didn't have the load factor and capacity parameters.
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are right; HashSet does use a Map as its backing and that does have a load factor. Sorry.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic