• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how many elements in a List

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using List on my program.
I suddenly wonder how many elements a List can add for itself.

anyone knows it?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList and Vector use an array for internal storage, so that's the limit to the number of objects it can hold. I believe it's Integer.MAX_VALUE - quite a lot. LinkedList has no limitation on the number of objects it can hold.

Of course both will run into memory problems before the limit will be reached.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Of course both will run into memory problems before the limit will be reached.



You also will be running into JVM restrictions. For example, the max size any object can be (for current JVMs) is 2GB. Since each reference is 4 bytes, an array of references, would exceed the 2GB restriction way before the maximum integer size.

Henry
[ November 26, 2008: Message edited by: Henry Wong ]
 
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question was about List, not necessarily ArrayList or Vector. Sure, ArrayList is the most common implementation found in most modern Java programs (die, Vector!) but it's far from the only one. Of course the List interface requires that methods like size() and get() use an int value, which means there's no way to ever have more than Integer.MAX_VALUE elements in a List. Much like Rob's agrument about ArrayList and Vector, except there's no reason to confine ourselves to those particular implementations - the Integer.MAX_VALUE limitation is more fundamental than that.

Beyond that - well, Henry's point about the maximum size of a single object may be relevant, if you're using an ArrayList. But again, let's not forget that there are other forms of List out there. A LinkedList, for example, does not suffer this particular limitation at all. And let's remember that some List implementations do not allow you to add elements at all. Try adding something to a List you get from Arrays.asList(), for example. Or Collections.unmodifiableList().

Once again, we have a question asked in Beginner, whose answer is perhaps beyond the scope of Beginner. That's unfortunate, but it happens. Maybe the poster only meant to ask about ArrayList - or maybe not. Either way, I don't think that understanding is served very well with oversimplified answers. If the only truthful answer is "the answer to that is too complicated for the Beginner forum", so be it.
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great post, with one slight mistake: it is quite much allowed for a List to have more than Integer.MAX_VALUE elements. According to the API:

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


If this is the case then that does mean that the elements after Integer.MAX_VALUE can only be accessed using an (List)Iterator, not using direct access.

But I can remember myself noting that LinkedList has virtually no limitation; you must have missed that
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. I was aware that Iterable had no limitation; I hadn't realized that List was written to allow that as well. Thanks for pointing that out.
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On further reflection though - how could such an oversized List implement the toArray() method according to its API? There doesn't seem to be a way to do this, strictly speaking. Of course, we live with the fact that, for example, TreeMap and TreeSet break their respective interface APIs (ignoring equals() checks in favor of compareTo() checks), and that doesn't seem to bother us most of the time. I suppose a supersized List could violate the API similarly, e.g. by simply throwing UnsupportedOperationException (even though toArray() was not specified as optional).
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a problem found in all Collection implementations, as it clearly specifies that toArray should return an array with all elements. Yet it also says the same about its size() as List does. So in fact, these two method specifications are contradictory if the size() is larger than Integer.MAX_VALUE!

So in order for all contracts to be maintained, the limit is in fact Integer.MAX_VALUE. Any higher and there is at least one contract broken - that of toArray.

Still, if 2,147,483,647 is not enough then I don't know what kind of monster you're creating
 
Mike Simmons
Rancher
Posts: 5184
84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed.
reply
    Bookmark Topic Watch Topic
  • New Topic