Solved

procesing HashMap

Posted on 2006-07-18
11
288 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

20 Experts available now in Live!

Get 1:1 Help Now