• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashmap doubt

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i got it..thanks sagar and ankit for clearing my doubt..
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried out to solve your problem using overridden hashCode() method ?
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sagar i did and it is printing three.
 
Whatever. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic