I am building a chat application. Current I have all messages in an ArrayList
, which got me thinking - How many elements are the ArrayList
design to hold? 100? 1.000? 10.000?

- 385
- 1
- 4
- 13
-
Beware of threading issues. – SLaks Jul 03 '13 at 14:18
-
Maybe this helps: http://www.coderanch.com/t/524745/java/java/Maximum-capacity-arrayList-String-objects – Andreas Fester Jul 03 '13 at 14:19
-
@MarounMaroun not everyone has enough rep to closevote. – CPerkins Jul 03 '13 at 14:23
4 Answers
The size of ArrayList
is Integer.MAX_VALUE
.
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* @return the number of elements in this list
*/
int size();
It is because ArrayList
uses array internally and theoretically an array can be of Integer.MAX_VALUE
in size at maximum. For further information, you can see this.

- 6,938
- 1
- 30
- 52
-
This doesn't answer the question at all. The JavaDoc on size just states the limitation on reporting of a list's size, it says *nothing* about the maximum size of a list. – SimonC Jul 03 '13 at 15:03
-
The answer is completely misleading, the `size()` JavaDoc gives no indication as to the maximum size of any list implementation. – SimonC Jul 03 '13 at 15:07
ArrayList which is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE.
A LinkedList isn't limited in the same way, though, and can contain any amount of elements.
see similar question max. length of List in Java
How many data a list can hold at the maximum to have other aspects on max size of list
-
2see http://stackoverflow.com/questions/7632126/maximum-size-of-hashset-vector-linkedlist – M Sach Jul 03 '13 at 14:43
-
ArrayList can hold any number of elements up to Integer.MAX_VALUE
- this is due to a design decision to use int
datatype for indexes. However what's important is how you are allocating memory for it - the memory allocation is slow - and how you're processing/accessing elements. From the storage aspect alone, though, you're limited by MAX_VALUE
. In Java, this is 2^31-1 = 2,147,483,647.
For any normal application this should be enough. Yet, if you need more, you can easily get the source code for it and modify it to use long
as the index datatype - and then be limited by Long.MAX_VALUE
.

- 56,435
- 29
- 168
- 265
-
The reason is not due to choosing int indices per se. The reason is due to the backing store of the List implementation which was chosen for its desired (amortised) algorithmic complexity of random access to the list (which requires array-like behaviour). In turn this backing store (an array) comes with this limitation in Java/JVM. However, one could implement the List interfaces to use negative indices as well giving the user 2^32 possible indexes using the same data type for index (`int`), for example by using two arrays and a few extra objects as backing store instead of the one array. – user268396 Jul 03 '13 at 14:43
-
@user268396 Fair enough, however you can still get the source for the `ArrayList` and modify it to increase the maximum size to whatever you want. Make the backing of it `Object[][]` for example and use `long` as the index - now you're limited by `Integer.MAX_VALUE ^ 2`. – Aleks G Jul 03 '13 at 14:46
-
Obviously, but my more general point is that in your answer (as stated) the reasoning misses the link between the limitation of the implementation and behaviour of the implementation (performance/algorithmic complexity). When taken at face value, a reasonable follow up question would be: why use ArrayList at all? What is it good for if all it can do is be a more limited version of `LinkedList`. To which the answer is "algorithmic complexity/performance", but then the limitation is not obvious nor explained. :) – user268396 Jul 03 '13 at 14:50