Replacing a HashSet Java member

前端 未结 5 1476
我在风中等你
我在风中等你 2021-01-07 16:47

I have Set of that structure. I do not have duplicates but when I call: set.add(element) -> and there is already exact element I would like the old to be replac

5条回答
  •  無奈伤痛
    2021-01-07 17:27

    If the set already contains an element that equals() the element you are trying to add, the new element won't be added and won't replace the existing element. To guarantee that the new element is added, simply remove it from the set first:

    set.remove(aWordInfo);
    set.add(aWordInfo);
    

提交回复
热议问题