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

后端 未结 7 767
野性不改
野性不改 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:38

    Your question: I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself.

    Just one Object variable is created for the entire datastructure of hashset and doing that would save yourself from re-writing the entire hashMap kind of code again.

    private static final Object PRESENT = new Object();

    All the keys are having one value i.e PRESENT object.

    0 讨论(0)
提交回复
热议问题