This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I return a counter for each key-value position in the Map?  RSS feed

 
Geane Norm
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have some code that iterates through key-value pairs in a map:



However, I am struggling to understand how to I can actually return a counter (integer) for each key-value position in the map, so that I would be returned with something like this:

Position: 1, Key one, Value: Jan

Can you help?

Thanks,

I.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Geane Norm
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:


Hi,
thanks for the quick reply, really appreciated. I have ended up writing the below code:



Thanks again for your help,

I.
 
Dave Tolls
Rancher
Posts: 2913
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since there is no ordering in a HashMap I'm not sure the idea of Position makes much sense?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with
System.out.println(myMap.keySet().size());   ?
You can replace keySet with entrySet if you prefer.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Since there is no ordering in a HashMap I'm not sure the idea of Position makes much sense?


It doesn't really make much sense, no. But maybe it's one of those homework things? The number of times I've asked "Why the X would you want to do that" and got the answer "My homework assignment says to do that"... I've given up asking that sort of question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!