Hash multipoint memory persistence problem

I am using HashMultiMap in my code. Now my code gets some bits periodically. Which I store in a string (eg String str = "0110011100"). And then convert it to int and store it as key / value of HashMultiMap. Is it possible to store it as bits instead of storing it as int / string? Does this save the map space? In fact, a string has more bits than a byte and less than an int (for example, for 14 bits). So I want to save space by keeping it as bits. Thank.


source to share

1 answer

Java has a handy class BitSet

that can store an almost unlimited number of bits. When the number of bits is large, this representation makes sense. However, when the number of bits is relatively small, this representation will use more space than an integer.

If the number of bits is limited to 32, the use BitSet

will be wasteful. With only 20 bits, you can create an array of sets 2^20

and not store all the keys at all. But this is considered a premature optimization.

The best way to approach this problem is to start with the view that is most convenient for you, which logically matches the design of your application. When your application is running, profile its memory usage to determine if you need to optimize the representation of your bitsets; chances are you don't need to do anything, at least not right away.



All Articles