Why does HashSet implementation in Sun Java use HashMap as its backing?

后端 未结 7 796
野性不改
野性不改 2020-12-01 02:47

Looking at the source of Java 6, HashSet is actually implemented using HashMap, using dummy object instance on every entry

7条回答
  •  囚心锁ツ
    2020-12-01 03:33

    My guess is that HashSet was originally implemented in terms of HashMap in order to get it done quickly and easily. In terms of lines of code, HashSet is a fraction of HashMap.

    I would guess that the reason it still hasn't been optimized is fear of change.

    However, the waste is much worse than you think. On both 32-bit and 64-bit, HashSet is 4x larger than necessary, and HashMap is 2x larger than necessary. HashMap could be implemented with an array with keys and values in it (plus chains for collisions). That means two pointers per entry, or 16 bytes on a 64-bit VM. In fact, HashMap contains an Entry object per entry, which adds 8 bytes for the pointer to the Entry and 8 bytes for the Entry object header. HashSet also uses 32 bytes per element, but the waste is 4x instead of 2x since it only requires 8 bytes per element.

提交回复
热议问题