Changing an object which is used as a Map key

前端 未结 5 1508
醉话见心
醉话见心 2020-12-21 07:33

Let V be a class with a single attribute named K and its getter and setters.

What\'s supposed to happen if I do:

V v = new V();
v.setK(\"a\");
HashMa         


        
5条回答
  •  遥遥无期
    2020-12-21 07:43

    If you were trying to look up the map using v.getk() it wouldn't find an entry, because you've changed the value return by the getter on v.

    Or in other words, the map isn't magically kept in sync with what happens to your v object - it uses (and keeps using) the value given to it in the put().

提交回复
热议问题