欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java中遍历HashMap的方法,java遍历hashmap,import java.

来源: javaer 分享于  点击 45786 次 点评:108

java中遍历HashMap的方法,java遍历hashmap,import java.


import java.util.*;  public class MapTest {      static HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        public static void main(String [] args) {          hashMap.put("one", 1);          hashMap.put("two", 2);          hashMap.put("three", 3);          hashMap.put("four", 4);          hashMap.put("five", 5);            Iterator iter = hashMap.entrySet().iterator();            // the first method to travel the map        while (iter.hasNext()) {              Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) iter.next();               String key = entry.getKey();              Integer value = entry.getValue();                 System.out.println(key + " " + value);          }            iter = hashMap.keySet().iterator();           // the second method to travel the map          while (iter.hasNext()) {              String key = (String) iter.next();              Integer value = hashMap.get(key);                  System.out.println(key + " " + value);          }      } // close main()  }  
相关栏目:

用户点评