How much data can a List can hold at the maximum?

JavaArraylistSize

Java Problem Overview


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

Is there any default size of an ArrayList?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

> 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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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);
          }

Solution 8 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - JavagustafcView Answer on Stackoverflow
Solution 2 - JavaBozhoView Answer on Stackoverflow
Solution 3 - JavaaioobeView Answer on Stackoverflow
Solution 4 - JavaGerco DriesView Answer on Stackoverflow
Solution 5 - JavaduffymoView Answer on Stackoverflow
Solution 6 - JavaMohammadView Answer on Stackoverflow
Solution 7 - JavaDead ProgrammerView Answer on Stackoverflow
Solution 8 - JavaOleksy OstaninView Answer on Stackoverflow