hashmap get key

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...
shragiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrcoffee365Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmalakarCommented:
Can you please show,  how are you  putting key value pairs in hash map ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.