Solved

procesing HashMap

Posted on 2006-07-18
11
287 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

12 Experts available now in Live!

Get 1:1 Help Now