Is there a use case for the size()
method on the java.util.BitSet
class?
I mean - the JavaDoc clearly says it's implementation dependant, it returns the size of the internal long[]
storage in bits. From what it says, one could conclude that you won't be able to set a bit with a higher index than size()
, but that's not true, the BitSet
can grow automatically:
BitSet myBitSet = new BitSet();
System.out.println(myBitSet.size()); // prints "64"
myBitSet.set(768);
System.out.println(myBitSet.size()); // prints "832"
In every single encounter with BitSet
I have had in my life, I always wanted to use length()
since that one returns the logical size of the BitSet
:
BitSet myBitSet = new BitSet();
System.out.println(myBitSet.length()); // prints "0"
myBitSet.set(768);
System.out.println(myBitSet.length()); // prints "769"
Even though I have been programming Java for the last 6 years, the two methods are always highly confusing for me. I often mix them up and use the wrong one incidentally, because in my head, I think of BitSet
as a clever Set<boolean>
where I'd use size()
.
It's like if ArrayList
had length()
returning the number of elements and size()
returning the size of the underlying array.
Now, is there any use case for the size()
method I am missing? Is it useful in any way? Has anyone ever used it for anything? Might it be important for some manual bit twiddling or something similar?
EDIT (after some more research)
I realized BitSet
was introduced in Java 1.0 while the Collections framework with most of the classes we use was introduced in Java 1.2. So basically it seems to me that size()
is kept because of legacy reasons and there's no real use for it. The new Collection classes don't have such methods, while some of the old ones (Vector
, for example) do.