12

What is the maximum length of List in java? I mean that how many maximum elements can store in list?

kandarp
  • 4,979
  • 11
  • 34
  • 43

3 Answers3

8
Integer.MAX_VALUE   

or heap which ever is low will be the limit

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    (-1) until the poster explains the contradiction between his answer and `List.size()` Javadoc (http://download.oracle.com/javase/6/docs/api/java/util/List.html#size%28%29): "If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE." – NPE Dec 21 '10 at 11:45
  • @musiKk The list interface's size() function returns an int. – helpermethod Dec 21 '10 at 11:45
  • @Helper: Read the documentation of said method (or my answer). – musiKk Dec 21 '10 at 11:46
  • @aix I believe Josh Bloch has stated that it was a mistake to specify `List` like that. I don't know of any implementations that do that, nor of any effort to make sure that the use of such a list would actually work or even fail nicely. – Tom Hawtin - tackline Dec 21 '10 at 11:48
  • @aix I am not saying that is the limit you can't go beyond this , but it will not function properly if you go beyond, although it certainly depends on the implementation of list,check [`toArray()`](http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T%5b%5d%29) documentation for more info – jmj Dec 21 '10 at 12:00
  • @Tom: This is beside the point. It doesn't matter what was intended and it doesn't matter what implementations are currently out there or known. If you really have to work with huge lists, you have to account for that. – musiKk Dec 21 '10 at 12:02
  • thanks to clear my doubt. But suppose, I have more element then Integer.MAX_VALUE, then will it become problem during iteration or any other operation over that list? – kandarp Dec 21 '10 at 12:09
  • [`toArray()`](http://download.oracle.com/javase/6/docs/api/java/util/List.html#toArray%28T%5b%5d%29)will stop working you need to check what functionality you need from List – jmj Dec 21 '10 at 12:10
  • @aix I hope I have answered your Question – jmj Dec 21 '10 at 12:11
  • @org.life.java OK, fair enough. I'll withdraw the vote if you edit the answer (any edit will do -- the vote's locked in otherwise.) – NPE Dec 21 '10 at 12:16
  • @musiKk If you have to work with huge sequences, then it'd be eccentric to stick with `List`. (Just over a year ago people it appears that some people started to hit problems with 2 GB arrays. I would expect 2^31 element lists to be some years off.) – Tom Hawtin - tackline Dec 21 '10 at 12:28
  • @org.life.java it is worth noting that many methods use toArray() or size() under the bonnet so you need to check the implementations, not just your code to see if that methdo will work. e.g. isEmpty() could fail. – Peter Lawrey Dec 21 '10 at 13:01
  • An example, Linkedlist just does size++; on an add so if you add 2^32 elements, isEmpty() will return true. :P – Peter Lawrey Dec 21 '10 at 13:09
  • @Peter Lawrey *true , Agree* :) – jmj Dec 21 '10 at 13:11
6

It's very likely that you will run out of heap space well before you get anywhere close to Integer.MAX_VALUE number of elements, but let's see what would happen if we had infinite memory and tried to add more than Integer.MAX_VALUE elements to a List:

1) ArrayList:

An ArrayList will try to increase its capacity:

int newCapacity = (oldCapacity * 3)/2 + 1;

This calculation will overflow and newCapacity will actually be less than Integer.MAX_VALUE. The ArrayList will then try to copy the elements in the original array to a smaller array and will hence lose elements.

2) LinkedList:

A LinkedList works a bit better once you cross the Integer.MAX_VALUE limit. It will continue to hold elements, however the size attribute will suffer from integer overflow and will affect other operations that make use of it.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
dogbane
  • 266,786
  • 75
  • 396
  • 414
3

This depends on the specific list implementation. Most people think it's Integer.MAX_VALUE but it isn't limited to that. In fact the documentation of the size() method merely states:

If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

So it might as well contain more elements.

musiKk
  • 14,751
  • 4
  • 55
  • 82
  • 1
    While the documentation says that, not all collections honour this. e.g. LinkedList just does size++ and size-- for addBefore() and remove(). There is no check against Integer.MAX_VALUE in the code. – Peter Lawrey Dec 21 '10 at 13:05
  • @Peter: I noticed that too. Bug? – musiKk Dec 21 '10 at 13:41
  • I am not completely sure Linkedlist can go over MAX_VALUE size in the first place. There is no simple way to test it without at least a 80 GB JVM. ;) (Each entry uses about 40 bytes on a 64-bit JVM) – Peter Lawrey Dec 21 '10 at 14:57