[Solved] ArrayList as a n implementation of List in Java 8


There are several scenarios to your question:

You have enough memory to hold both the original array and a larger copy

Then it’s the usual scenario, the capacity is automatically increased and you can add more elements.

You have few heap memory at your disposal.

In that case, you’ll get an OutOfMemoryError when ArrayList tries to create a copy of its internal array with a bigger capacity.

You already have a capacity around Integer.MAX_VALUE - 8 and you have enough heap memory to create an even bigger array.

Then, you’ll get an OutOfMemoryError if you’re using a Hotspot/OpenJDK JVM. It’s not documented anywhere in the List interface or their implementations, but according to tests Holger realized (see comments), this is what you get because creating an array of size Integer.MAX_VALUE throws such error. Other JVMs might throw other exceptions or errors.

4

solved ArrayList as a n implementation of List in Java 8