Changing values in HashSet

前端 未结 3 2082
滥情空心
滥情空心 2020-12-29 15:20

I\'ve read this question: Changing the elements in a set changes the \'equals\' semantics

However, I don\'t know how to solve the problem that I can\'t change an ite

3条回答
  •  旧巷少年郎
    2020-12-29 15:58

    You are facing the problem because the keys in your hashset are not immutable. If you don't have immutable keys, you will lose the reference of the original key object once modified. And will never be able to get handle of that, which is sometimes referred as memory leaks in collection. So if you use immutable keys, you wouldn't run into this situation.

提交回复
热议问题