• Post Reply Bookmark Topic Watch Topic
  • New Topic

LinkedHashMap- When to specify size and Load Factor  RSS feed

 
Uttam Kini
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am pretty new to JavaRanch ... this is my first post

I want to know whether specifying the initial size and load factor in the constructor of a LinkedHashMap affects the iteration speed or just the insertion spped. In my case insertion into the map is done once and the rest of the time only accesses happen. So I am not sure if specifying the size and load factor will give me any performance gains.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only time I�d set the initial size and load factor is if profiling showed it made a significant difference on production data. The recent JavaRanch journal had an article about hash maps, and the initial size didn�t seem to make much difference.
 
Uttam Kini
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link .... that solves my problem ... Since I have a few thousand String keys ... the initial size wouldn't matter.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have several thousand keys, then it probably is worthwhile specifying an initial size. Otherwise you will start off with the default size, which is to be found in the LinkedHashMap documentation. So (for 3000 pairs) you would double the size of your Map when you reach 12, 24, 48, 96, 192, 384, 768 and 1536 elements, whereas specifying an initial size of 3000 would give a capacity of 4096 (at least) automatically.
Remember for the default load factor of 0.75f you might do well to specify a size of 1.3333333 times how many elements you want.
 
Uttam Kini
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Point taken .... But the LinkedHashMap documentation also says that since it is internally maintained as a linked list (unlike HashMap), the iteration times stay unaffected regardless of the initial capacity ... my application sort of caches data from the database during startup and then many objects use the static reference of the map to access data .... So a slight increase in startup time would not do my app much harm.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!