Tuning collection to hold large number of objects

删除回忆录丶 提交于 2019-12-10 16:39:23

问题


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


回答1:


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);



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4686894/tuning-collection-to-hold-large-number-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!