This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Unspecified problem with a Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unspecified problem with a Map" Watch "Unspecified problem with a Map" New topic
Author

Unspecified problem with a Map

Santhosh Kumar T R
Greenhorn

Joined: Oct 28, 2009
Posts: 3
Hi,

Here i have a doubt. I have written a following code and i am unable to understand what is wrong with it?



Thanks,
Santhosh


Cheers,
Santhosh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Santhosh Kumar T R wrote:Hi,

Here i have a doubt. I have written a following code and i am unable to understand what is wrong with it?


What specific problem are you having with it? What do you expect it to do, and what is it doing instead?
Santhosh Kumar T R
Greenhorn

Joined: Oct 28, 2009
Posts: 3
Thanks. I am sorry and will remember next time

I wanted to know if the code i have written the iterate a hashMap is correct or not?
Because i am seeing entrySet() being used in the older posts.

If something is wrong please do let me know why exactly we should use entrySet instead of my method? Maybe if someone let me know which is the efficient way and why?
I tried to search in the google but couldnt find a satisfactory answer

Thanks,
Santhosh


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Whether it is correct or not depends on what you want to do.

In your example, you iterate over the values of the map, and you print just the values. You could also iterate over the keys, by using keySet() instead of values() or the key-value pairs (entries), by using entrySet() instead of values().

Another tip: I would use the shorter and simpler new for-syntax:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesper de Jong wrote:Whether it is correct or not depends on what you want to do.

In your example, you iterate over the values of the map, and you print just the values. You could also iterate over the keys, by using keySet() instead of values() or the key-value pairs (entries), by using entrySet() instead of values().


And you can find all this out by simply reading the docs for those methods.
Santhosh Kumar T R
Greenhorn

Joined: Oct 28, 2009
Posts: 3
Thanks for the reply.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unspecified problem with a Map
 
Similar Threads
hash map getting null or only last value
geting only l;ast vale from hashmap
HashMap, Iteraring problem
Create hashmap and dynamically populate
HashMap with Class as value