Solved

procesing HashMap

Posted on 2006-07-18
11
291 Views
Last Modified: 2010-08-05
I have stonred some values in HashMap.
I am having trouble getting values back.

Please shere some code to process HashMap objects...
0
Comment
Question by:jaipur07
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17130268
Say you want String 'X':


String X = (String)map.get("X");
0
 
LVL 35

Expert Comment

by:girionis
ID: 17130269
Hi jaipur07

you simply do Object o = hashmap.get("key"); and this should give you the object that is identified by the key "key"

Cheers
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17130273
If you have declared HashMap as

Map map = new HashMap();
map.put("Name", "ABC"); // Puts Name to hashmap
String value = (String) map.get("Name"); //Returns the value of Name
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jaipur07
ID: 17130296
no no i don't want value based on a key...i want to get all key value pairs from hashmap
0
 
LVL 35

Expert Comment

by:girionis
ID: 17130313
Iterator iter = HashMap.keySet().iterator();
while(iter.hasNext()) {
    System.out.println(iter.next());
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 17130325
Iterator iter = HashMap.keySet().iterator();
while(iter.hasNext()) {
    String s = iter.next();
    System.out.println(s);
    System.out.println(HashMap.get(s));
}
0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 17130343
this

> String s = iter.next();

needs a cast

String s = (String) iter.next();

0
 
LVL 35

Expert Comment

by:girionis
ID: 17130353
:)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17130357
Hi jaipur07,

You can get keys and values using entrySet() :

Iterator it=hashmap.entrySet().iterator();
while (it.hasNext())
{
     Map.Entry entry=(Map.Entry)it.next();
      ...  entry.getKey()         // is the key
      ...  entry.getValue()      // is the value associated to the key
}
0
 

Author Comment

by:jaipur07
ID: 17130363
thanks!!
you guys are awsome...i wish i could award 10000000 points
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17130641
jaipur07, please try to be more precise in your questions - it will save time.

For getting the pairs, Webstorm's approach is preferable
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

752 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