Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Jan 12, 2005



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Increase Instance Capacity by Ten Percent

ArrayList doesn't need any introduction to Java programmers. It is probably the most used component of the Java collection framework.

One of the ArrayList constructors takes Collection as an argument. This constructer is supposed to create a new ArrayList a copy of the given collection. However, if you add an additional object or a new instance, it may not necessarily expand the internal data structure. Why?

Because when an ArrayList object is constructed using an existing collection object, it will create an ArrayList instance with ten percent more capacity then the original collection (limited by maximum integer value). This increase will not be reported by the size() API. This undocumented increase provides higher performance by avoiding memory reallocation in case a few more objects were added to this instance.

Knowing this undocumented feature can save some time for performance-sensitive application programmers—especially when ArrayList doesn't provide a constructor with a collection object and initialCapacity value.

Amit Jain
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date