iterating over elements of a HashMap

Hi,

I made a HashMap<String, Integer>. I want to iterator over all elements.

for (Iterator it = m_Table.values().iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry)it.next();
    String strKey = (String)entry.getKey()
    Integer nData = (Integer)entry.getValue() ;
}

This causes a runtime exception though... what's the right way to do it?

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:

try this

for (Iterator it = m_Table.entrySet().iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry)it.next();
    String strKey = (String)entry.getKey()
    Integer nData = (Integer)entry.getValue() ;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ajay-SinghCommented:

for(Map.Entry<String,Integer> entry: m_Table.entrySet()) {
  String key = entry.getKey();
  Integer value = entry.getValue();
}

Open in new window

0
DJ_AM_JuiceboxAuthor Commented:
Hmm they work. But I don't get the order in which the keys are extracted. In my HashMap, I have keys:

"xy"
"z"

That was the order in which they were entered into the map too. But when I iterate over them, they come out in:

"z"
"xy"

order?

Thanks
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ksivananthCommented:
hashmap don't maintain the order, try using tree map!
0
ksivananthCommented:
sorry looks like, that will keep in a sorting order!
0
cmalakarCommented:
HashMap will not return the entries in order in which they were inserted..

If you want the same order in which you inserted, then you have to use.. LinkedHashMap
0
ksivananthCommented:
try use LinkedHashMap!
0
DJ_AM_JuiceboxAuthor Commented:
Ok I wrote my whole app (which is not that big really) using HashMap. The only operations I used from HashMap were get() and put().

So by switching the LinkedHashMap, I shouldn't have to change any of my use calls, right? The only difference is that now when I iterate over the elements, they'll come out in the order inserted?

Thanks
0
cmalakarCommented:
Yes...
Just replace new HashMap()

With LinkedHashMap()

0
cmalakarCommented:
LinkedHashMap class is inherited from HashMap class
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.