Benefits of creating an ArrayList with an initial capacity of 0?

I am an experienced Java developer and I continue to see such things.

List<Integer> l = new ArrayList<Integer>(0); 

which I really can’t understand. What is the point of creating an ArrayList with an initial capacity of 0, when you know that it will grow above capacity?

Are there any known advantages to this?

+6
source share
6 answers

It keeps the size (in memory) of the ArrayList very small and is a tactic when you want a variable to be non-NULL and ready to use, but don't expect the List to populate immediately. If you expect it to be filled immediately, it is better to set it to a larger initial value - any "growing" ArrayList internally creates a new primitive array and copies the elements. The growth of an ArrayList expensive and should be minimized.

Or, if you create many instances of the class, each of which contains one of these List properties. If you do not plan to fill them out immediately, you can save some memory without having to allocate a room.

However: there is a better way: Collections.emptyList() . Usually you want to protect access to this list directly and (as an example) in your class provide method calls for a specific domain that work with the internal List . For example, let's say you have a School class that contains a List of student names. (Simply put, note that this class is not thread safe. )

 public class School { private List<String> studentNames = Collections.emptyList(); public void addStudentName(String name) { if (studentNames.isEmpty()) { studentNames = new ArrayList<String>(); } studentNames.add(name); } public void removeStudentName(String name) { studentNames.remove(name); if (studentNames.isEmpty()) { studentNames = Collections.emptyList(); // GC will deallocate the old List } } } 

If you are ready to perform isEmpty() checks and perform initialization / assignment, this is the best alternative to creating a large number of empty ArrayList instances, since Collections.emptyList() is a static instance (there is only one) and is not mutable.

+7
source

For java 6 (or openjdk 7), without specifying the initial size, you will get a list from the initial size set to 10. Thus, depending on many factors you use the list, this can be very little more memory and / or efficiency to initialize list size 0 .

For java 7, specifying an initial size of 0 functionally equivalent to not specifying an initial size.

However, it is actually less efficient, since calling the constructor with argument 0 calls new Object[0] , whereas if you specify the no-args constructor, the initial elementData for your list will be set to a statically defined constant named EMPTY_ELEMENTDATA .

Relevant code from ArrayList source:

 /** * Shared empty array instance used for empty instances. */ private static final Object[] EMPTY_ELEMENTDATA = {}; 

In other words, using new ArrayList<Integer>(0); seems redundant, there are no advantages to this, and instead I would use new ArrayList<Integer>(); .

+7
source
  • If the additions to this ArrayList really unlikely, and if it is important to keep the size of the ArrayList at least, I see that it is useful.

  • Or, if the sole purpose of this ArrayList is to return a value from a method, where returning an empty list is a special message to the calling function, for example, "no results were found."

  • Otherwise, in fact.

+3
source

By default, ArrayList has a capacity of 10 and each time changes by + 50%.

Using a lower initial capacity, you can sometimes (theoretically) save memory. On the other hand, each size of time takes a lot of time. In most cases, this is just a sign of proactive optimization.

+2
source

It is always better suited to get a large value (if you exceed the number of transitions) in the list of arrays, because this will reduce the size of the list and, therefore, optimize the execution time.

To initialize the list of arrays with a value of 0 create an Empty list of arrays, which is reducing memory , if you know that your list will no longer contain 10 .

+1
source

Depending on the contract, you can avoid NullPointerExceptions without having zeros. This is good practice in certain situations; see Effective Java by Joshua Bloch Point 43: Returns empty arrays or collections, not nulls

-2
source

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


All Articles