• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

hashmap getting key+value

Hi,
Is it possible to iterate through hashmap and get key and value?

0
static86
Asked:
static86
  • 4
1 Solution
 
for_yanCommented:


What do you mean by this quuestion?
I guess hashmap is designed for that - you iterate through the keys
and then retrive the value corresoponding to the key
and if you wich you can concatenate them, for example.

0
 
for_yanCommented:
Is this somethin that you meant:

HashMap m;

Enumeration keys = m.keySet();
Iterator it = m.iterator();
while(it.hasNext()){
Object ob = it.next();
Object value = m.get(ob);
System.out.println(ob.toString() + "  "  + value.toString());
}


0
 
for_yanCommented:

This is with corrections:

       
HashMap m = new HashMap();

Set keys = m.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
Object ob = it.next();
Object value = m.get(ob);
System.out.println(ob.toString() + "  "  + value.toString());
}

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
for_yanCommented:



       
HashMap m = new HashMap();

// populate HashMap in some way ...

Set keys = m.keySet();
Iterator it = keys.iterator();
while(it.hasNext()){
Object ob = it.next();
Object value = m.get(ob);
System.out.println(ob.toString() + "  "  + value.toString());
}

Open in new window

0
 
static86Author Commented:
Thanks a lot!
0
 
CPColinSenior Java ArchitectCommented:
If you know you're going to be getting both the key and the value anyway, it's usually better to iterate over HashMap.entrySet() instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now