2

Possible Duplicate:
How many data a list can hold at the maximum

What is the maximum number of elements a list can hold?

Community
  • 1
  • 1
Sanjay Jain
  • 3,518
  • 8
  • 59
  • 93
  • possibly duplicate http://stackoverflow.com/questions/3767979/how-many-data-a-list-can-hold-at-the-maximum – Enrique Dec 18 '10 at 06:19

3 Answers3

1

Assuming you mean implementations of the java.util.List interface, methods like get() and size() use int, so the upper theoretical boundary would be Integer.MAX_VALUE entries. You might run out of memory before you reach this limit though!

The index type in Java arrays is int too, so you're definitely limited to Integer.MAX_VALUE entries for regular arrays.

Kim Burgaard
  • 3,508
  • 18
  • 11
  • 2
    Since Java doesn't offer unsigned ints, I don't see how any implementation can offer more than 2^31-1. – Gabe Dec 18 '10 at 06:22
  • @Gabe correct, while you might be able to do something sensible with get(int), size() would not make much sense with negative numbers. I'll update my answer – Kim Burgaard Dec 18 '10 at 06:25
1

If you're talking about an ArrayList, they're indexed using integers (which are always signed in Java), so they can theoretically hold 2^31 elements (not 2^32). At that point you're probably going to have memory issues anyway.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • 1
    @Matt - Unless I'm mistaken they would be in the range `0..2^31-1` inclusive, which would still be `2^31` total elements – Brad Mace Dec 18 '10 at 06:28
  • Yup, [you're totally right](http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE). Sorry! I need to not be on SO while sleep-deprived. – Matt Ball Dec 18 '10 at 06:47
  • I wouldn't worry about memory issues. If my 64-bit laptop comes with 4GB of memory (allowing any process to easily fit an array of more than 2^31 bytes in its working set), a 32GB desktop is easily imaginable, servers with 256GB should be commonplace soon. – Gabe Dec 18 '10 at 06:59
1

LinkedList is also an implementation of List which stores the elements as a Linked List. So theoretically its size is equivalent to the amount of memory you can allocate.

Faisal Feroz
  • 12,458
  • 4
  • 40
  • 51
  • I don't entirely agree. The java.util.List interface fix the index type to int, so operations like size(), get(int), set(int, E), add(int, E) and remove(int) would not make sense for lists with more than Integer.MAX_VALUE elements. – Kim Burgaard Dec 18 '10 at 06:32