views:

1481

answers:

6

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.
+3  A: 
Stu Thompson
+1  A: 

Yes, with BigInteger class.

Joonas Pulakka
+8  A: 

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.

Dave Webb
+2  A: 

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
+2  A: 

A memory upgrade is necessary.. :)

ZiG
+1  A: 

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