This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Unspecified problem with a Map

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic