Question

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.
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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 :)

Yes, with BigInteger class.

A memory upgrade is necessary.. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top