Over 500 Magazines for FREE!

Yes! You can subscribe to ALL of them. They will be shipped to your home FREE of cost!
Kindly click here to apply!

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. */

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?

Post new comment

To combat spam, please enter the code in the image.




Do you have a better example?
We're sure you have hundreds of Java program examples.

Spare some time and submit your java example here even if you think it's too small to contribute.

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




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