?
Solved

hashmap getting key+value

Posted on 2011-04-22
6
Medium Priority
?
310 Views
Last Modified: 2012-06-21
Hi,
Is it possible to iterate through hashmap and get key and value?

0
Comment
Question by:static86
  • 4
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35446189


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
 
LVL 47

Expert Comment

by:for_yan
ID: 35446199
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35446212

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35446216



       
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
 

Author Closing Comment

by:static86
ID: 35446220
Thanks a lot!
0
 
LVL 14

Expert Comment

by:CPColin
ID: 35448531
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question