Java HashSet vs HashMap

后端 未结 7 1784
长发绾君心
长发绾君心 2020-12-12 16:13

I understand that HashSet is based on HashMap implementation but is used when you need unique set of elements. So why in the next code when putting

7条回答
  •  不思量自难忘°
    2020-12-12 16:42

    I think the major difference is, HashSet is stable in the sense, it doesn't replace duplicate value (if found after inserting first unique key, just discard all future duplicates), and HashMap will make the effort to replace old with new duplicate value. So there must be overhead in HashMap of inserting new duplicate item.

提交回复
热议问题