4

Setting aside the heap's capacity, are there ways to go beyond Integer.MAX_VALUE constraints in Java?

Examples are:

  1. Collections limit themselves to Integer.MAX_VALUE.
  2. StringBuilder / StringBuffer limit themselves to Integer.MAX_VALUE.
IAdapter
  • 62,595
  • 73
  • 179
  • 242
setzamora
  • 3,560
  • 6
  • 34
  • 48
  • Which exact constraints are you talking about? – Jon Skeet May 12 '09 at 10:59
  • 1
    No. of elements in a collection, StringBuilder / StringBuffer capacity, etc. – setzamora May 12 '09 at 11:00
  • 3
    There's no way to increase the number of elements in a single collection beyond it's indexes max_value (except rewriting the collection class). In practice, any real world memory limits are certainly exceeded way before the max_value... – Joonas Pulakka May 12 '09 at 11:05
  • 4
    You do realise that Integer.MAX_VALUE is a few billion ? Why on earth would want need to create a String with more than a billion chars ? – mP. May 12 '09 at 11:08
  • I don't understand why you would have more than MAX_VALUE elements in your collection...If you go in this situation you have a conception problem. – Valentin Rocher May 12 '09 at 11:08
  • Collection *do not* limit themselves to Integer.MAX_VALUE elements. `size()` returns an `int`, so that's the biggest that it can return, but the docs for [`size()`](https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#size--) explicitly mention that the collection might have more elements. – Joshua Taylor Jul 15 '18 at 23:39

6 Answers6

9

If you have a huge Collection you're going to hit all sorts of practical limits before you ever have 231 - 1 items in it. A Collection with a million items in it is going to be pretty unwieldy, let alone one with more than a thousands times more than that.

Similarly, a StringBuilder can build a String that's 2GB in size before it hits the MAX_VALUE limit which is more than adequate for any practical purpose.

If you truly think that you might be hitting these limits your application should be storing your data in a different way, probably in a database.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • While the pragmatic constraint is true (if you actually have that many elements, you probably want something like a database), Collections *can* have more than Integer.MAX_VALUE elements. The docs for [`size()`](https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#size--) explicitly call this out: "Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE." And it's easy to have conceptually more elements. E.g., if you subclass,AbstractList. – Joshua Taylor Jul 15 '18 at 23:38
3

With a long? Works for me.

Edit: Ah, clarification of the question. Cool. My new and improved answer:

With a paging algorithm.

Coincidentally, somewhat recently for another question (Binary search in a sorted (memory-mapped ?) file in java), I whipped up a paging algorithm to get around the int parameters in the java.nio.MappedByteBuffer API.

Community
  • 1
  • 1
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
3

You can create your own collections which have a long size() based on the source code for those collections. To have larger arrays of Objects for example, you can have an array of arrays (and stitch these together)

This approach will allow almost 2^62 elements.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Array indexes are limited by Integer.MAX_VALUE, not the physical size of the array.

Therefore the maximum size of an array is linked to the size of the array-type.

byte = 1 byte => max  2 Gb data
char = 2 byte => max  4 Gb data
int  = 4 byte => max  8 Gb data
long = 8 byte => max 16 Gb data

Dictionaries are a different story because they often use techniques like buckets or an internal data layout as a tree. Therefore these "limits" usually dont apply or you will need even more data to reach the limit.

Short: Integer.MAX_VALUE is not really a limit because you need lots of memory to actually reach the limit. If you should ever reach this limit you might want to think about improving your algorithm and/or data-layout :)

Philipp
  • 7,199
  • 1
  • 22
  • 16
1

Yes, with BigInteger class.

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
1

A memory upgrade is necessary.. :)

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74