• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Iterate over a hashMap

 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to iterate over two the hashMaps where I need the combination of elements from both the hashMaps. The iteration must be something similar to bubble Sort

Instead of using an ArrayList I need to use a HashMap which holds the value and its associated count.I am unable to figure out what to use in place of indexes(i and j)
Please help me
Thanks in advance
 
Pavel Kubal
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you itereate over keys and through keys you can get values?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using legacy code like Vector and StringTokenizer?
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why are you using legacy code like Vector and StringTokenizer?


Would you please explain it in detail.
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavel Kubal wrote:Why don't you itereate over keys and through keys you can get values?


Hey it would be great if you could help me with some sample code.
Please translate the above code and help me.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need the key-value pairs, then don't iterate over the keys and get the values. It's more efficient to iterate over the entries of the map, so that you don't have to do a lookup in the map for each key. Each entry is a key-value pair. For example:

About Campbell's remark: Class Vector is a legacy collection class. It has been replaced by ArrayList since Java 1.2. There's no good reason anymore to use the old class Vector. Instead of StringTokenizer, you can use the newer and more convenient class Scanner.
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here instead of vector Candidates I have to use a hashmap and iterate similarly.
Please help me how to iterate over this loop with hashmap.
Just help me convert this code as required.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read java.util.HashMap . and Jesper explained well to your query already
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:

map does not implement Iterable so you can't use it directly in a for-each loop. You surely mean map.entrySet()
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob, my mistake, it should have been:
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jesper, for answering my question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic