Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Default load factor 0.75. why ?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Why the default load factor of HashMap/HashSet is 0.75 instead of 1. How is it helpful?


Thanks
Ramdas.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramdas,

A very Good question...

I saw the API,

What i understood:

if you have initial capacity as 100. and load factor set to 0.75,
then if 75 buckets are full, then it will start executing rehash,
which will cause the capacity to be doubled like 200.

rehashing needs some time to re-locate(expand) the Map.

thats why we keep it .75 by default. not to 1.


I didn't get why HashMap, Hashsets have loadfactor why not ArrayList.
Please correct me if i am in a illusion...
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When adding/retrieving from collections that use hashing to allocate storage it's quite possible to get 'collisions', making it slow to allocate and retrieve. If you waited till it was completely full before expanding it's very likely you'd have had many, many collisions - so to avoid this they do it early.

But not too early as you might end up wasting memory, and there could be other reasons (maybe getting all the values in a list would take time when 90% of the spaces are empty).

ArrayLists don't use hashing, and so waiting for the current allocation to fill up doesn't cost anything.
[ March 15, 2007: Message edited by: Andy Morris ]
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

This is a good conversation, AND it's important to know that this topic isn't on any version of the SCJP exam.

Bert
 
I will suppress my every urge. But not this shameless plug:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic