• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Initial Capacity of ArrayList

 
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any logic which can help us in deciding ideal initial capacity of an ArrayList if we know the approximate no. of data entries to be stored?
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what's wrong with just using what size you think it needs to be?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you only know the approximate number of elements you want to add to an ArrayList the best thing you can probably do is use this approximate number as an initial capacity and perhaps add some more just to be sure. In the end you are trying to avoid many reallocations inside the list, so for large numbers of elements I'd say an approximate number is better than nothing.

Marco
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But still is there any way to find out the optimal initial capacity or it is just hit and trial?
 
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "optimal" initial capacity would depend on what it is that you want to optimize. If you clarified that, you might see your way towards the answer.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankush, you have to be aware that there's almost always not the one and only optimal solution which fits all situations. Depending on your needs and requirements there are often trade-offs and you have to decide on a case-by-case basis what could be an optimal solution for your problem.

Regarding your example with ArrayList it could be optimal to calculate the exact inital size for the ArrayList and avoid excessive reallocation of memory. But under certain circumstances it could be better to avoid preallocation of too much memory and use a low initial size. I'd even argue that for the average application in a typical environment which handles only a moderate amount of data and doesn't have special performance requirements the optimal 'solution' is to keep the code as simple as possible and simply ignore the initial size at the cost of potential reallocations of memory.

Marco
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankush Kaundal wrote:But still is there any way to find out the optimal initial capacity...?


One other thing that's worth noting: the array that you're trying so hard to "get right" is an array of references, not an array of objects. On my old clunker that means that for every bit of "capacity" that I save, I'm saving 4 bytes. On 64-bit Java that might be 8 (not sure how it works out references).

So: if I'm out by 1,000? I've wasted 4-8k. My Dell dinosaur has 3 Gig to play with. Even most cellphones these days have at least 32 Meg, so it's a drop in the ocean.

What's slightly more important - but only slightly - is avoiding re-allocations, since that involves not only allocating a new array, but copying the contents of the old one.

So my usual rule of thumb: if you can, make sure that your estimate is always an overestimate.

Winston
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any logic which can help us in deciding ideal initial capacity of an ArrayList if we know the approximate no. of data entries to be stored?



Yes, there is!
Please cover my tutorial to know ArrayList profoundly.
You will find answer not only to this but to many other situations
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic