?
Solved

hashmap get key

Posted on 2010-03-26
2
Medium Priority
?
471 Views
Last Modified: 2013-12-15
I wrote entry set to get all the values from hash map...


   Set<Map.Entry<String, Integer>> set2 = m.entrySet();
              for (Map.Entry<String, Integer> me : set2) {
                  System.out.print(me.getKey() + ": ");
                  System.out.println(me.getValue());
                  
                  




but for getting value for a key I use

System.out.println("B's key : " + m.get("hi"));

so it prints output ike

B's key : 5

but when I do the below operation...

int k = m.get("hi"):

System.out.print(k); // here i get diff answer becoz m.get("hi") is an object..


but this can be done in entryset

int ttt = me.getValue();

here i get the correct answer... but I cannot pass on arguments...

is there a way to find the value for a specific key...

like m.get("hi"); and store it in integer value...

I tried to type cast object into string and to integer but i got diff answer...


i even tried this one...

System.out.println("B's key : " + m.get("hi"));
                           
              int dsk =m.get("hi").intValue();
             
              System.out.print("keyyyyyy  dsk "+dsk);
the output that I got...

B's key : 4
keyyyyyy dsk 49629

both are not same...
0
Comment
Question by:shragi
2 Comments
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 28727821
I believe you're seeing Java math and type conversion in action.  It doesn't have to do with HashMap, it has to do with Integer, int, and automatic type conversion.  This site has excellent explanations about the many flavors of the problem.
http://mindprod.com/jgloss/intvsinteger.html
http://mindprod.com/jgloss/convert15.html
http://mindprod.com/jgloss/round.html

O'Reilly has a pretty good outline of the different ways to do type conversion:
http://docstore.mik.ua/orelly/java-ent/jnut/ch02_04.htm
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 28739969
Can you please show,  how are you  putting key value pairs in hash map ?
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

600 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