adsense


Showing posts with label JAVA EXAMPLES ITERATE THROUGH HASH MAP. Show all posts
Showing posts with label JAVA EXAMPLES ITERATE THROUGH HASH MAP. Show all posts

Saturday, 31 October 2015

JAVA EXAMPLES ITERATE THROUGH HASH MAP PAGE 163

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES ITERATE THROUGH HASH MAP










Java Examples - Iterate through Hash Map



Problem Description:
How to iterate through elements of HashMap?
Solution:
Following example uses iterator Method of Collection class to iterate through the HashMap.
import java.util.*;

public class Main {
   public static void main(String[] args) {
      HashMap< String, String> hMap = 
      new HashMap< String, String>();
      hMap.put("1", "1st");
      hMap.put("2", "2nd");
      hMap.put("3", "3rd");
      Collection cl = hMap.values();
      Iterator itr = cl.iterator();
      while (itr.hasNext()) {
         System.out.println(itr.next());
     }
   }
}
Result:
The above code sample will produce the following result.
3rd
2nd
1st