Solved

procesing HashMap

Posted on 2006-07-18
11
289 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
windows explorer path to command prompt 5 42
how to add new optional parameter to JSP 1 40
Java string replace 11 43
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now