Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterating a HashMap !  RSS feed

 
Gunjan Rathi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hava 2 hashmaps..the entrySet for first is like [a=10, b=20, c=30] and that for the second is [a1=2, b1=4, c1=3]...
Now I want to add the values of first map i.e 10+20+30...
and add the values of second i.e 2+4+3...
And my final desired output is (10+20+30)/(2+4+3)....
Now how to get it done using these 2 hashmaps...meaning how to iterate and get the output...Thanks in advance !
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the entrySet() method if each HashMap which gives you a Set of map.Entry objects.

Get an Iterator with the iterator() method, and iterate the Set.
Use the getValue() method to find the values you want.

Remember to use generic typing for the Iterator etc to avoid class casting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!