• 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

How to get values from Hash Map in optimised way

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I stored some information in HashMap and getting back that information as shown below. See the following code.

---------------------------------------------------
HashMap mInfo = queryResponse.getInfo();
if(mInfo==null) mInfo = new HashMap();
Set set = mInfo.entrySet();
Iterator im = set.iterator();
while(im.hasNext())
{
Map.Entry me = (Map.Entry)im.next();
if(mId.equals((String)me.getKey()))
{
mName = (String)me.getValue();
break;
}
}
---------------------------------------------------
See as shown above, I got the information back. But is there any other way to do same but bit optimistic? Help me posting the answer back...!!


Best Regards
------------
Shashi Kala
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My goodness, friend, you seem not to have understood the point of Maps. The "get" method returns the value associated with a given key, if any; otherwise it returns null. So you can say, for example,

 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ernest,

Yes!! null check I should do..But why I did not do so means, I know that, if any key value is equal to mid, there is a value too..because those values I put in Hash map previous..Any ways, But Its good to check for null condition. I will change it.

But, to get key is there any other way, instead of using getKey() method??

Regards,

Shashi Kala
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry! Ernest,

I misunderstood what you said. Now I got it. Thanks you very much.

Regards,

Shashi Kala
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that a value can be null too. If you want to allow this if it was set, use containsKey as well:
 
reply
    Bookmark Topic Watch Topic
  • New Topic