简体   繁体   中英

Most efficient way to manage large amount of boolean state

I want to manage the boolean state (on/off) of up to 10^18 items. What is the most memory and computationally efficient way of doing this in Java? I can't create an array of booleans this large as the primitive type of the size of an array is int , and the same goes for the BitSet class.

Eg:

long numSwitches = Long.MAX_VALUE;
boolean[] switches = new boolean[numSwitches];

gives me a compilation error: Incompatible types, Reguired: int, Found: long

I honestly don't think something like this is possible without the use of a supercomputer. 10^18 is a very big number, it's actually relatively close to the number of atoms in a single grain of salt! If each atom is 1 bit, then the total memory will be an upwards of 125 petabytes (125,000 terabytes)!

Even if you wish to access a bit sequentially (random access will require you to load it all into memory) via the use of some sort of stream, it would still take a ridiculous amount of time.

Unless only a few bits compared to the total amount will be enabled at a time (a few billion, which would still require gigabytes of memory), I don't see any plausible way to solve this unfortunately. However, it's still very interesting to think about.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM