This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Collections_maps  RSS feed

 
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?
 
author and jackaroo
Posts: 12197
279
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!