aspose file tools*
The moose likes Beginning Java and the fly likes How to get values from Hash Map in optimised way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get values from Hash Map in optimised way" Watch "How to get values from Hash Map in optimised way" New topic
Author

How to get values from Hash Map in optimised way

Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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,



[Jess in Action][AskingGoodQuestions]
Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
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

Joined: Jan 27, 2008
Posts: 46
Sorry! Ernest,

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

Regards,

Shashi Kala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Please note that a value can be null too. If you want to allow this if it was set, use containsKey as well:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get values from Hash Map in optimised way
 
Similar Threads
how to cache a CMP-Bean
HashMap with Class as value
Method Hash
runtime exception during accessing elements in hashmap
how to iterate a hashmap with key hashmap