Solved

procesing HashMap

Posted on 2006-07-18
11
292 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

617 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