boolean[] vs. BitSet: Which is more efficient?

Maxim picture Maxim · Mar 3, 2009 · Viewed 41k times · Source

What is more efficient in terms of memory and CPU usage — an array of booleans or a BitSet? Specific BitSet methods are not used, only get/set/clear (==, =, Arrays.fill respectively for an array).

Answer

Peter Lawrey picture Peter Lawrey · Mar 3, 2009
  • Boolean[] uses about 4-20 bytes per boolean value.
  • boolean[] uses about 1 byte per boolean value.
  • BitSet uses about 1 bit per boolean value.

Memory size might not be an issue for you in which case boolean[] might be simpler to code.