tags:

views:

108

answers:

6

How much data can be added in java.util.List in Java at the maximum Is there any default size an array list?

+1  A: 

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

duffymo
+3  A: 

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
@Bozho are you sure, i don't think it is.. if this is the case then int can also hold -ve value
org.life.java
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
@musiKk, @org.life.java - you are right, fixed it.
Bozho
According to docs the elements must fit in an array (See toArray method).
aioobe
@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
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
that one no, but you know there are list implementations that don't support some operations :)
Bozho
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
+4  A: 

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
According to docs the elements must fit in an array (See toArray method).
aioobe
+2  A: 

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

The interface java.util.List has no documented limit on the maximum number of elements of a class that implements this interface. However, I suspect that a class allowing for more than Integer.MAX_VALUE (231-1 = 2147483647) elements would have trouble implementing certain methods faithfully, such as List.toArray which according to documentation should...

Return an array containing all of the elements in this list in proper sequence (from first to last element);

... since the size of an array is limited by Integer.MAX_VALUE.

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.

aioobe
+1  A: 

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.

gustafc
A: 

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);
          }
Suresh S
duffymo
@duffymo i was trying to say when u create a ArrayList like this List l = new ArrayList(); the default size would be ten.
Suresh S
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