File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashmap doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashmap doubt" Watch "hashmap doubt" New topic
Author

hashmap doubt

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
I got this from javabeat.net



Here if the comment the line 1 then i getting output as one 3
but if i uncomment that line then it is giving null 3

Here i thought if hashcode() is not overridden then it will store all the values in the same bucket. So while retriving the values it checks its equals method. Here (if uncommented the line 1) it return true for ob3 also then why not it is printing three?
Please clear my doubt

Thanks,
Geeta. V
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

geeta vemula wrote:Here i thought if hashcode() is not overridden then it will store all the values in the same bucket.


No! It will just do the opposite. Since the hashCode method is not overridden, so it will place all values in different buckets as hashCode method of Object class mostly returns unique values for objects...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
ok then if i comment out the line 1 then how it is printing one . it should not search in that particular bucket?? if it is searching that bucket means it is also searching bucket with key ob3, there also equals method is returning false.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I didn't get your point. Let me modify the code



Now I have removed some of the code. Now it will add one Ques73 object to the map and retrieve it. Now lets add the statements back to the program



Now it will add three objects to the map and find the first one. Now why do you think obj3 comes into the picture?? The output would be One 3...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1


You make the two object with same parameter "1", that's dosen't mean you created ( or refereeing ) the same object, there no Constant Pool for class Ques73.

So on line 1


You assigned the altogether different object to "ob1", And which is ought to be different from one you inserted..

When you retrieved using reference "ob1", he can't find that object..

BUT, you're question is Good and really intrigued.. I think , I'm missing somewhere....

Override the Object#hashCode() method in your class, and we can get as expected by you ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
Ankit I am saying in the program if i add one line as below



then according to overidden equal method ob1 equals ob3(int hashCode is same) then why not it is printing "three" ?

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Yes you are right that obj1 and obj3 are equal according to the equals method. But since the hashCode method is not overridden, so the hashCode method of object class will be called. It will return different hashCode for obj1 and obj3. That's what the whole point is. obj1 and obj3 will go into different bucket...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Sagar Rohankar wrote:
You make the two object with same parameter "1", that's dosen't mean you created ( or refereeing ) the same object, there no Constant Pool for class Ques73.

So on line 1


You assigned the altogether different object to "ob1", And which is ought to be different from one you inserted..

When you retrieved using reference "ob1", he can't find that object..

BUT, you're question is Good and really intrigued.. I think , I'm missing somewhere....

Override the Object#hashCode() method in your class, and we can get as expected by you ..
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
ok i got it..thanks sagar and ankit for clearing my doubt..
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Have you tried out to solve your problem using overridden hashCode() method ?
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
yes sagar i did and it is printing three.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashmap doubt