• Post Reply Bookmark Topic Watch Topic
  • New Topic

load factor  RSS feed

 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • 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..
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
By size you mean the individual bucket size or the entire Set's size?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
    • Quote
    • Report post to moderator
    Thanks bala,ritchie!
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • 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.
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • 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: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are right; HashSet does use a Map as its backing and that does have a load factor. Sorry.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!