• 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

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: 9050
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic