-2

BitSet internally uses long array of size 6.

But it can contain 2^31-1 bits.

long = 64 bit

6 longs = 64 * 6. But is much less than 2^31-1. Please explain this trick.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

4

The internal array which stores the values expands if necessary, just like an ArrayList would do for objects. Your statement is therefore false.

There is a hard limit of 2^31-1 (bits) for the size, but that's only because the operations (set, flip, etc.) take the index parameter as an int. Which is probably big enough for most use cases.

Kayaman
  • 72,141
  • 5
  • 83
  • 121