This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 12195
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.
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!