I understand HashSet
based on HashMap
, since they are pretty similar. It makes the code more flexible and minimizes implementation effort. However, one reference variable in the HashSet's Entry
seem to be unnecessary for me if the class forbids null
element, therefore the whole Entry makes no point. Despite this fact, an Entry
takes 24 byte memory / element, whereas a single array with the set's elements would take only 4 byte / element if my figures are correct. (aside from array's header)
If my argument is correct, does the advantages really overweight this performance hit?
(if i am wrong, i would learn from it aswell)
Though this question is primarily opinion-based, I'll summarize a few points on the topic:
HashSet
appeared in Java 1.2 many years ago. It's hard to guess now the exact reasons for design decisions made at that times, but clearly Java wasn't used for high-loaded applications; performance played less role than simplicity.HashSet
is suboptimal in its memory consumption. The problem is known, the bug JDK-6624565 is registered, and discussions at core-libs-dev are held from time to time. But is this a blocker for many real world applications? Probably, no.HashSet
memory usage is unacceptable, there are already good alternatives, like trove THashSet.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With