Set up a collection to store a large number of objects

If a collection, like an arraialist, stores thousands of user objects (for example, Person with several properties), do something in my code or in the collection constructor to prepare it for such a large collection.

I really don't think about dedicated threads, etc., but more in line with load factors (do I need to touch this for the above scenario?).

thanks

+4
source share
3 answers

I would just initialize the collection to a size that would be close to the final size in order to minimize the number of changes:

List<Person> persons = new ArrayList<Person>(1024); 
+6
source

Another approach:

Since we are talking about such a huge collection that would β€œeat” your RAM,
I think you should consider storing this collection in a database and reading / writing / updating ONLY when you need to.

+11
source

You can do:

 new ArrayList<T>(10000); 

which pre-allocates an array with the specified size (for example, 10000), so it does not need to redistribute when adding elements. In addition, you can not do anything. In addition, it does not matter for ArrayList which link it stores, so the information cannot help you in optimization.

+8
source

Source: https://habr.com/ru/post/1335492/


All Articles