Why does Java's Collection.size() return an int? This limits the size of collections to just over 2 billion entries. With the rapidly increasing amounts of memory available to us, this seems a little short-sighted - no?
migrated from stackoverflow.com Sep 16 '11 at 4:18
|
The vast majority of applications uses collection sizes much less than that. A generic class should be designed to cover the most common cases, not the most extreme ones. A specialized collection class for very large collections will probably be added at some point. |
|||||||||||||||||||||
|
|
The javadoc for the
Therefore there isn't any inherent limit on the size of a |
|||||||||
|
|
IMO people can reasonably disagree about whether The point is when you have collections of billions of elements, the precise size is most likely unimportant. A sense of the size will most likely get you there. As a sidenote, precise counts often get expensive, and is why |
|||
|
|
The real answer is to maintain backwards compatibility with older versions of Java. The Now 64bit JVMs are commonplace, and huge collections are at least feasible. But if they changed Now you could argue with some justifications that collections that big have implementation and performance issues. Those issues could be worked by implementing special collection classes. If you do this, then you can also give the class (say) a |
||||
|
|
|
Worrying about index numbers is old fashioned. The future looks more like the below:
Now that I think about it. They have already implemented a suitable collection - Map.
|
|||||||||||
|
|
And who knows? Maybe when 2 billion is too small, they would have updated Java so an int holds more, or make a new primitive type. |
|||||||||
|
|
there is a problem to store all array values into RAM so such big lists will cause usage of swap file and make system too slow, i think it is a bad idea. You'd better use more suitable structures to store such amount of data |
|||||||||||||||||
|
|
Obviously, Java Collection is mostly made for collections that contain 2 billion entries at most. If you have more entries to manage, chances are that you need something else than, say, an ArrayList. Nothing keeps you from implementing and using such a collection. The only issue is that the standard collection interface is not the best possible match for that beast. |
|||
|
|
|
I think they did the right thing. Like everything else to do with computers Algorithms don't necessarily scale. An hash/storage algorithm which handles a 10,000 items nicely is probably going to die long before it hits 500,000,000 items. And its not just about Moores law. Core counts are doubling about every five years and while a single threaded "copy" is OK on a set of a few thousand for a set of millions you would want to activate all the available cores. I think the original "collections" are still fit for purpose as most collections in the real world will hold less than a 1000 entries, and there are relatively few > 10,000. Someday just like BigDecimal and BigNumber we will have BigHash and HugeArray. |
|||
|
|

Thing. Let's see... I'll make it support a bazabegig items. Yeah. Oh, wait! Someday somebody might want to add a degahedricbazigadon items - I'll design it for that instead! But what if they want to add a beezelbubdoodlezigahedron items someday?" Where do you stop to actually have something you can use? And why wouldn't you consider 2 billion items in aCollectionto be enough? If you have more items than that, is aCollectionreally the best way to store and use it? Is the item count really the limit, or the data itself? – Ken White Sep 16 '11 at 1:14Collectiondoesn't seem to be a limit, even if the individual item only consumed a small amount of memory. (And in the early '90s, 2 billion bytes of memory was huge - PCs then had RAM sizes in MB. I know - in '94 I got my first monster PC with 16MB RAM and a 1GB SCSI drive. The drive was $1000 used, and the RAM was too.) – Ken White Sep 16 '11 at 1:26