76

How much data can be added in java.util.List in Java at the maximum?

Is there any default size of an ArrayList?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Paul
  • 1,380
  • 3
  • 12
  • 21

8 Answers8

78

It depends on the List implementation. Since you index arrays with ints, an ArrayList can't hold more than Integer.MAX_VALUE elements. A LinkedList isn't limited in the same way, though, and can contain any amount of elements.

Edit: I realize this sounds like an endorsement of LinkedList. But please note that although they don't have a theoretical capacity limit, linked lists are usually an inferior choice to array-based data structures because of higher memory use per element (which lowers the list's actual capacity limit) and poor cache locality (because nodes are spread all over RAM). It's definitely not the data structure you want if you have more items than can be crammed into an array.

gustafc
  • 28,465
  • 7
  • 73
  • 99
  • 9
    What would you have size(), get(int), set(int, E) and remove(int) do for lists with more than Integer.MAX_VALUE elements? Due to those constraints, I would think the upper limit of elements will always be Integer.MAX_VALUE because the index type is imposed by the java.util.List interface. – Kim Burgaard Dec 18 '10 at 06:29
  • 14
    The [spec for `size()`](http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#size%28%29) clearly says: *Returns the number of elements in this list. If this list contains more than `Integer.MAX_VALUE` elements, returns `Integer.MAX_VALUE`.* Indexing operations won't work for elements after index `Integer.MAX_VALUE`, but unless the implementation relies on indexing (which `ArrayList` does out of necessity), you could still iterate over the list, add things to it (either at the tail or before index `Integer.MAX_VALUE`), remove elements (again, not after `Integer.MAX_VALUE`), etc. – gustafc Dec 20 '10 at 08:04
  • I got this question wrong on a exam ( I said it was true) and according to you , its only true as a LinkedList: `ArrayList cannot resize dynamically if you add more number of elements than its capacity.` . Am I correct then and the exam was wrong? (it was a ucertify.com exam) – djangofan Dec 11 '16 at 20:36
  • @djangofan I'm not sure what your question is, or what how the question you got wrong was phrased, but here's the deal: The `List` interface _does not_ specify a maximum size, but all implementations will be limited in some way. `ArrayList`, for example, is limited by the [maximum size of arrays](http://stackoverflow.com/a/31388054/58956), while `Collections.EMPTY_LIST` has a maximum capacity of zero elements because that's how its specification is, etc. (And all implementations are of course limited by memory available, unless they have some kind of out-of-RAM infinitely size storage). – gustafc Dec 14 '16 at 08:50
  • I'm not sure that what you say about LinkedList having no element storage limits is true because when I try to do `linkedList.get(index)` and index is a long variable type, the IDE tells me that I can only use int as the index that I'm looking for which means that LinkedList is also limited to 2^31 elements, no? – Michael Sims Jan 25 '23 at 07:29
  • @MichaelSims This has already been discussed in comments above, see my comment from 2010-12-20. – gustafc Jan 25 '23 at 08:27
  • @gustafc - as a side note, I attempted to create a `LinkedList` then looping from 0 to `Integer.MAX_VALUE` I tried to fill it with empty Strings `""` but no matter how much memory I gave the JVM, it starts slowing to a crawl and seemingly stopping around 1 billion adds ... wanted to test the limit but no dice apparently. – Michael Sims May 19 '23 at 13:44
29

It would depend on the implementation, but the limit is not defined by the List interface.

The interface however defines the size() method, which returns an int.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit

ArrayList (which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • @Bozho are you sure, i don't think it is.. if this is the case then int can also hold -ve value – jmj Sep 22 '10 at 09:42
  • I think that's wrong. Or else why would `Collection#size()` say: _If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE._ This depends on the specific implementation. – musiKk Sep 22 '10 at 09:42
  • 1
    According to docs the elements must fit in an array (See toArray method). – aioobe Sep 22 '10 at 10:50
  • @aioobe - the method will throw an exception, because the `size` field will be negative (due to int overflow), but the list itself will work. – Bozho Sep 22 '10 at 11:02
  • Well, depends on how you define "a list that works". Is an implementation that does nothing but throwing an exception in each List-method a working list? – aioobe Sep 22 '10 at 11:19
  • that one no, but you know there are list implementations that don't support some operations :) – Bozho Sep 22 '10 at 11:28
  • hehe, ok :) In the API? So I guess the maximum size of a list depends on where you draw the line for what a correct list is :-) – aioobe Sep 22 '10 at 12:23
13

How much data can be added in java.util.List in Java at the maximum?

This is very similar to Theoretical limit for number of keys (objects) that can be stored in a HashMap?

The documentation of java.util.List does not explicitly documented any limit on the maximum number of elements. The documentation of List.toArray however, states that ...

Return an array containing all of the elements in this list in proper sequence (from first to last element); would have trouble implementing certain methods faithfully, such as

... so strictly speaking it would not be possible to faithfully implement this method if the list had more than 231-1 = 2147483647 elements since that is the largest possible array.

Some will argue that the documentation of size()...

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

...indicates that there is no upper limit, but this view leads to numerous inconsistencies. See this bug report.

Is there any default size an array list?

If you're referring to ArrayList then I'd say that the default size is 0. The default capacity however (the number of elements you can insert, without forcing the list to reallocate memory) is 10. See the documentation of the default constructor.

The size limit of ArrayList is Integer.MAX_VALUE since it's backed by an ordinary array.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
10

java.util.List is an interface. How much data a list can hold is dependant on the specific implementation of List you choose to use.

Generally, a List implementation can hold any number of items (If you use an indexed List, it may be limited to Integer.MAX_VALUE or Long.MAX_VALUE). As long as you don't run out of memory, the List doesn't become "full" or anything.

Gerco Dries
  • 6,682
  • 1
  • 26
  • 35
  • According to docs the elements must fit in an array (See toArray method). – aioobe Sep 22 '10 at 10:50
  • @aioobe One can always throw an `UnsupportedOperationException` for `toArray()`. – MC Emperor Apr 12 '21 at 08:52
  • @MCEmperor right, that's possible, since it's an unchecked exception. Note however: many `List` methods are documented to (possibly) throw `UnsupportedOperationException`. `toArray` is _not_ one of them. – aioobe Apr 13 '21 at 06:43
  • @aioobe Yes, I noticed that as well. I think that's a little strange, because I think it's not up to the `List` interface how something is implemented, but to the implementing class instead. But apparently, the API designers had something different in mind. – MC Emperor Apr 13 '21 at 06:59
4

As much as your available memory will allow. There's no size limit except for the heap.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

The interface however defines the size() method, which returns an int.

Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

So, no limit, but after you reach Integer.MAX_VALUE, the behaviour of the list changes a bit

ArrayList (which is tagged) is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE

Mohammad
  • 121
  • 1
  • 3
0

see the code below of arraylist default it is 10 when u create List l = new ArrayList();

   public class ArrayList<E> extends AbstractList<E> implements List<E>,
           Cloneable, Serializable, RandomAccess {

          private static final long serialVersionUID = 8683452581122892189L;

          private transient int firstIndex;

          private transient int lastIndex;

          private transient E[] array;

          /**
           * Constructs a new instance of {@code ArrayList} with ten capacity.
           */
          public ArrayList() {
              this(10);
          }
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • And if you try to add the eleventh element, what happens? (Hint: It succeeds.) Your answer is "correct", since you cut & pasted this out of source, but it implies a max of ten elements, which is not correct. – duffymo Sep 22 '10 at 11:12
  • @duffymo i was trying to say when u create a ArrayList like this List l = new ArrayList(); the default size would be ten. – Dead Programmer Sep 22 '10 at 11:26
  • 3
    I know, but that's not what the question asked about. It had to do with the max allowable size of an array, not the initial size. See the difference? – duffymo Sep 22 '10 at 11:51
-1

Numbering an items in the java array should start from zero. This was i think we can have access to Integer.MAX_VALUE+1 an items.