How does HashSet not allow duplicates?

后端 未结 6 1983
情歌与酒
情歌与酒 2021-02-06 23:17

I was going through the add method of HashSet. It is mentioned that

If this set already contains the element, the call leaves t

6条回答
  •  无人共我
    2021-02-06 23:31

    See HashMap#put:

    Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.

    It replaces the key with the new value, this way, no duplicates will be in the HashSet.

提交回复
热议问题