Arpssss
Arpssss

Reputation: 3858

Hash Multi-Map Space Saving Issue

I am using HashMultiMap in my code. Now, my code is getting periodically some bits. Which I store in a string (ex. String str = "0110011100"). And then convert it to an int and store it as my HashMultiMap key/value. Is it possible to store it as bits instead of storing it as int/string ? Is that way saves the space of the map ? Actually, the string has more bits than byte and less than int (say for ex. 14 bits). So, I want to save space by storing it as bits. Thanks.

Upvotes: 3

Views: 200

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

Java has a convenient BitSet class which can store a virtually 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 would use more space than an integer.

If the number of bits is limited to 32, using BitSet is going to be wasteful. With only 20 bits you could potentially make an array of 2^20 sets, and avoid storing the keys altogether. But this counts as premature optimization.

A better way to approach this problem is to start with the most convenient representation for you, that fits your application design logically. When the application is working, profile its memory usage to determine if you need to optimize the representation of your bit sets; more likely than not, you wouldn't need to do anything about it, at least not right away.

Upvotes: 5

Related Questions