Java HashSet Performance

I understand HashSet based HashSet , as they are pretty similar. This makes the code more flexible and minimizes implementation effort. However, one reference variable in the HashSet Entry seems unnecessary to me if the class prohibits the null element, so the whole record makes no sense. Despite this fact, Entry takes up 24 bytes of memory / element, while a single array with installed elements will only accept 4 bytes / element if my numbers are correct. (except array header)

If my argument is true, are the benefits really overweight with this performance?

(if I am wrong, I would learn from him)

+6
source share
1 answer

Although this question is mainly opinion based, I will summarize a few related questions:

  • HashSet appeared in Java 1.2 many years ago. It’s hard to guess the exact reasons for making design decisions at that time, but it’s clear that Java was not used for highly loaded applications; performance is less important than simplicity.
  • You are right that HashSet is suboptimal in memory consumption. The problem is known, error JDK-6624565 is registered , and discussions on core-libs-dev are held from time to time. But is this a blocker for many real-world applications? Probably no.
  • For those rare applications where HashSet memory usage is unacceptable, there are already good alternatives, such as trove THashSet .
  • Note that open addressing algorithms have their drawbacks, for example. a significant decrease in performance with load factors close to 1; difficulty removing items. See the answer.
+1
source

All Articles