Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go beyond Integer.MAX_VALUE constraints in Java

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.
like image 843
setzamora Avatar asked Dec 02 '22 07:12

setzamora


2 Answers

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.

like image 74
Dave Webb Avatar answered Dec 03 '22 22:12

Dave Webb


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.

like image 26
Stu Thompson Avatar answered Dec 03 '22 21:12

Stu Thompson