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

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

    I am guessing that it has never turned up as a significant problem for real applications or important benchmarks. Why complicate the code for no real benefit?

    Also note, that object sizes are rounded up in many JVM implementation, so there may not actually be an increase in size (I don't know for this example). Also the code for HashMap is likely to be compiled and in cache. Other things being equal, more code => more cache misses => lower performance.

提交回复
热议问题