• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unspecified problem with a Map

 
Santhosh Kumar T R
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic