Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuning collection to hold large number of objects

If a collection, like an arraylist, will be storing custom objects (eg Person with several properties) in the thousands, is there anything to do in my code or in the constructor of the collection to prepare it for such a large collection.

I'm not really thinking of dedicated threads etc, but more along the lines of the load factor (do I need to touch this for the above scenario?).

Thanks

like image 422
GurdeepS Avatar asked Nov 29 '22 03:11

GurdeepS


2 Answers

A different approach:

Since we are talking about such a Huge Collection, that would "Eat up" you RAM,
I think you should consider storing this collection in a database and read/write/update ONLY when you must.

like image 135
athspk Avatar answered Dec 07 '22 02:12

athspk


You can do:

new ArrayList<T>(10000);

which pre-allocates the array with the specified size (e.g 10000) so that it doesn't have to re-allocate as you add elements. Apart from that, there is nothing you can do. Also - it doesn't matter to the ArrayList what kind of reference it is storing, so that information can't really help you in optimisation.

like image 29
gub Avatar answered Dec 07 '22 02:12

gub