开发者

Tuning collection to hold large number of objects

开发者 https://www.devze.com 2023-02-03 23:08 出处:网络
If a coll开发者_C百科ection, 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 col

If a coll开发者_C百科ection, 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


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.


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.


I'd just initialize the collection to a size that would be close to the final size, in order to minimize the number of resizings:

List<Person> persons = new ArrayList<Person>(1024);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号