Initializing an ArrayList with a new operator in Java?

What is the best practice for initializing an ArrayList in Java?

If I initialize an ArrayList using the new operator, then the ArrayList will by default have the memory allocated for 10 buckets. This is a performance hit.

I don’t know, maybe I'm wrong, but it seems to me that I should create an ArrayList , specifying the size, if I am sure of the size!

+4
source share
8 answers

This is a performance hit.

I would not worry about the "hit of productivity". Creating objects in Java is very fast. The difference in performance is unlikely to be measurable.

In any case, use the size if you know it. If you don’t do it, there’s nothing to be done.

The kind of thinking you are doing here is called "premature optimization." Donald Knuth says this is the root of all evil.

Better to get your code working before doing it fast. Optimize the data that tells you where your code is slow. Do not guess - you are probably mistaken. You will find that you rarely know where the bottlenecks are.

+5
source

If you know how many elements you add, initialize an ArrayList with the correct number of objects. If you do not, do not worry about it. The difference in performance is probably not significant.

+3
source

This is the best advice I can give you:

  • Do not worry about it. Yes, you have several options for creating an ArrayList , but using new , the default provided by the library, is not a BAD choice, otherwise it would be foolish to do it by default for everyone without specifying which is better.
  • If it turns out that this is a problem, you will quickly find it in the profile. This is the right place to look for problems when profiling an application for performance / memory issues. When you write code for the first time, you don’t worry about it - it’s a premature optimization - you just worry about writing good, clean code with a good design.
  • If your design is good, you should fix this problem in no time, which has little effect on the rest of the system. Effective Java 2nd Edition, paragraph 52: access objects by their interfaces. You can even switch to LinkedList or any other List there if this proves to be a better data structure. Design for this flexibility.
  • Finally, effective Java 2nd Edition, paragraph 1: consider static factory methods instead of constructors. You can even combine this with paragraph 5: avoid creating unnecessary objects unless you really need new instances (for example, Integer.valueOf does not always create a new instance).

Related Questions


At ArrayList Micromanagement

Here are some specific tips if you need to micromanage an ArrayList :

  • You can use ArrayList(int initialCapacity) to set the initial capacity of the list. If necessary, the list will automatically exceed this volume.
  • When you are going to fill / add to an ArrayList , and know what the total number of elements is, you can use ensureCapacity(int minCapacity) (or directly above the constructor) to reduce the amount of intermediate growth. Each add will work in an amortized constant time regardless of whether you are doing this (as guaranteed by the API), so this can only reduce the cost by a constant factor.
  • You can trimToSize() to minimize storage usage.

This type of micromanagement is usually not needed, but if you decide (justified by the final profiling results) that it is worth the hassle, you can do it.

see also

+3
source

If you already know the size of your ArrayList (approximately), you should use a constructor with capacity. But most of the time, the developers really do not know what will be on the List, so a capacity of 10 should be enough for most cases.

10 buckets is an approximation and is not a performance hit, if you do not already know that your ArrayList contains many elements, in this case the need to resize the array all the time will be performance.

+1
source

You do not need to specify the initial size of the ArrayList. You can always easily add / remove any item.

If this is a performance issue, remember the following things:

  • ArrayList initialization is very fast. Do not worry about it.
  • Adding / removing an element from an ArrayList is also very fast. Do not worry about it.
  • If you find that your code is too slow. The first to blame is your algorithm, no offense. Machine specifications, OS, and language are really involved. But their participation is considered insignificant in comparison with your participation in the algorithm.
+1
source

If you do not know the size of the ArrayList , then you are probably better off using LinkedList , since the operation LinkedList.add() is a constant speed.

However, since most of the people here said that you should not worry about speed before doing any kind of profiling.

You can use this old but good (in my opinion) article for reference. http://chaoticjava.com/posts/linkedlist-vs-arraylist/

+1
source

Since ArrayList is implemented using the underlying array , we must choose the initial size for the array.

0
source

If you're really interested, you can call trimToSize () after you create and populate the object. Javadoc states that the capacity will be no less than the size of the list. As said earlier, it is unlikely that you will find that the memory allocated for ArrayList is a bottle of performance, and if that were the case, I would recommend that you use an array.

0
source

All Articles