Iterate through the values of Java HashMap example

  1. /*
  2.   Iterate through the values of Java HashMap example
  3.   This Java Example shows how to iterate through the values contained in the
  4.   HashMap object.
  5. */
  6.  
  7. import java.util.Collection;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10.  
  11. public class IterateValuesOfHashMapExample {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. //create HashMap object
  16. HashMap hMap = new HashMap();
  17.  
  18. //add key value pairs to HashMap
  19. hMap.put("1","One");
  20. hMap.put("2","Two");
  21. hMap.put("3","Three");
  22.  
  23. /*
  24.   get Collection of values contained in HashMap using
  25.   Collection values() method of HashMap class
  26.   */
  27. Collection c = hMap.values();
  28.  
  29. //obtain an Iterator for Collection
  30. Iterator itr = c.iterator();
  31.  
  32. //iterate through HashMap values iterator
  33. while(itr.hasNext())
  34. System.out.println(itr.next());
  35. }
  36. }
  37.  
  38. /*
  39. Output would be
  40. Three
  41. Two
  42. One
  43. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

More modern alternative

I would recommend to use something like:

  1. for (Object key: hMap.keySet()) {
  2. System.out.println(hMap.get(key));
  3. }

Or even better with a hMap using generics:
  1. for (Map.Entry<int, String> entry: hMap.entrySet()) {
  2. System.out.println(entry.getValue());
  3. }

Very helpful! Thanks!

Very helpful! Thanks!

hm

why is it not working if i try to work with the values which itsself are objects of another class ?

  1. Collection c = allLendings.values();
  2. Iterator itr = c.iterator();
  3.  
  4. while(itr.hasNext())
  5. {
  6. String[] date = itr.next().getLend().split(".");
  7. if(date[1] == month)
  8. {
  9. . ....
  10. }
  11. }

Iterator returns object

Hi,

Iterator's next method returns object. So you need to cast it to appropriate class before invoking its method.

Hope this helps.
Thanks.

This is better

  1. Iterator hashIterator = hashMapName.keySet().iterator();
  2. while(hashIterator.hasNext()) {
  3. Type variable = hashIterator.next();
  4. Type2 variable2 = hashMapName.get(variable);
  5. ...
  6. }

I like the first one better

Because it is compact

  1. for (Object key: hMap.keySet()) {
  2. System.out.println(hMap.get(key));
  3. }

very good

Thanks for the better example.
It is easy to read and very compact.

Why not just use: for (

Why not just use:

  1. for ( Object x : hMap.values() ) {
  2. System.out.println(x);
  3. }

is it bad?

good examples really

good examples really

another question

how can i sort by a attrubite from objact that is a value in a map


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!