• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections_maps  RSS feed

 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
source: K&B book.. chap 7:

i wanted to clarify about a piece of code, I am not understanding properly..
pasting only relevant parts of code:


in the last case, i am asking it -> m.get(new Dog("clover"))
so the constructor of Dog is called with the name as clover and is returned to m.get method.. why isn't the equal method happy with the object? from where is d1 coming into picture there even if I am not sending a d1?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12152
256
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh - how unusual ... Kathy & Bert being extra tricky.

To understand this, you have to understand a couple of things:
  • how Java stores keys and values in a Map (a reference to an object, or a copy of the object (which in turn requires an understanding of passing by reference or passing a copy)).
  • what this means if the key or the value is a mutable object
  • Does that give you a hint as to what might be happening?

    If not, you might want to add this snippet to the MapTest class:

    Then call this method every time something is added to the map, and every time that d1 is modified.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!