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?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
migration rejected from stackoverflow.com Oct 24 '15 at 21:10
This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.
The javadoc for the
Therefore there isn't any inherent limit on the size of a
The real answer is to maintain backwards compatibility with older versions of Java.
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
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.