• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Doubt in Hash Map

 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I was trying one of the examples in K&B chapter Generics and collection

The code is:


The line System.out.println(m.get("DOG")); should return the object but it returns null.
I cannot figure it out why..
Can somebody help? :roll:
 
author
Posts: 23945
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The line System.out.println(m.get("DOG")); should return the object but it returns null.
I cannot figure it out why..
Can somebody help?




It would help us, if you tell us... what object do you think it should return?

Henry
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should return the dog object d1 .... Isn't it?
[ December 14, 2008: Message edited by: Sudipto Shekhar ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i don't know how you missed it
If you are clear on what put(key, value) takes as an argument and
what get(key) takes as an argument then I bet you'll be 100% clear.

Let me try and explain:
m.put(d1,"DOG");
| |
| |
\/ \/
key value

m.get(d1));
//this returns DOG because it searched for the value put against the key d1
//which you did put in earlier.

m.get("DOG");
//this returns null because it searched for the value put against the key
//"DOG" which you never put in earlier.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i got it..
Thank you very much...
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic