| 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: 24061
|
|
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: 19216
|
|
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
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: How to get values from Hash Map in optimised way
|
|
|