This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Issue in Working with Maps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Issue in Working with Maps " Watch "Issue in Working with Maps " New topic

Issue in Working with Maps

Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
Hi friends,

In the above code the marked line 7 also giving the null, I don't know why it is giving null, because I have defined
both the hashcode and equlas method in the Dog class.

Thanks in Advance,
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
The culprit here is line 52, in which you are changing the name of d1 object. So when you try to retrieve from map the hashcode method will return different value since the name is changed and hence desired object will not be found.

My Techie Blog - Java Questions
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3371
It is a best practice to use immutable obejcts as your key. You can use the Wrapper classes.

[] | [Java job hunting know how] | [Java resumes]
I agree. Here's the link:
subject: Issue in Working with Maps
It's not a secret anymore!