Java如何遍历Map的所有的元素

来源:互联网 发布:java有多少个类 编辑:程序博客网 时间:2024/05/01 16:14

JDK1.4中

[java] view plaincopyprint?
  1. <font color="#0000ff">Map map =new HashMap();
  2.  
  3.     Iterator it = map.entrySet().iterator(); 
  4.  
  5.     while (it.hasNext()) { 
  6.  
  7.         Map.Entry entry = (Map.Entry) it.next(); 
  8.  
  9.         Object key = entry.getKey(); 
  10.  
  11.         Object value = entry.getValue(); 
  12.  
  13. }</font> 

JDK1.5中,应用新特性For-Each循环

[java] view plaincopyprint?
  1. Map m = new HashMap(); 
  2.  
  3. for(Object o : map.keySet()){ 
  4.  
  5.     map.get(o); 
  6.  

返回的 set 中的每个元素都是一个 Map.Entry 类型。

[java] view plaincopyprint?
  1. <font color="#0000ff">private Hashtable<String, String> emails =new Hashtable<String, String>();</font> 

  另外 我们可以先把hashMap 转为集合Collection,再迭代输出,不过得到的对象

[java] view plaincopyprint?
  1. <font color="#0000ff">//方法一: 用entrySet() 
  2.  
  3.    Iterator it = emails.entrySet().iterator(); 
  4.  
  5.    while(it.hasNext()){ 
  6.  
  7.     Map.Entry m=(Map.Entry)it.next(); 
  8.  
  9.     logger.info("email-" + m.getKey() +":" + m.getValue()); 
  10.  
  11.    } 
  12.  
  13.    
  14.  
  15.    // 方法二:jdk1.5支持,用entrySet()和For-Each循环() 
  16.  
  17.    for (Map.Entry<String, String> m : emails.entrySet()) { 
  18.  
  19.     
  20.  
  21.     logger.info("email-" + m.getKey() +":" + m.getValue()); 
  22.  
  23.    } 
  24.  
  25.    
  26.  
  27.    // 方法三:用keySet() 
  28.  
  29.    Iterator it = emails.keySet().iterator(); 
  30.  
  31.    while (it.hasNext()){ 
  32.  
  33.     String key; 
  34.  
  35.     key=(String)it.next(); 
  36.  
  37.     logger.info("email-" + key +":" + emails.get(key)); 
  38.  
  39.    } 
  40.  
  41.  
  42.  
  43. // 方法五:jdk1.5支持,用keySEt()和For-Each循环 
  44.  
  45.  
  46.  
  47. for(Object m: emails.keySet()){ 
  48.  
  49.     logger.info("email-" + m+":" + emails.get(m)); 
  50.  
  51.    } 
  52. </font> 

      Map    aa    =    new    HashMap();       aa.put("tmp1",    new    Object());      //追加      替换用同样的函数.       aa.remove("temp1");                        //删除       for    (Iterator    i    =    aa.values().iterator();    i.hasNext();    )    {               Object    temp    =    i.next();       }          //遍历  

  

来个完整的,包含TreeSet的元素内部排序的

[java] view plaincopyprint?

  1. public staticvoid main(String[] args) { 
  2.  
  3.    ArrayList<String> list = new ArrayList<String>(); 
  4.  
  5.    HashMap<Object,Object> hash = new HashMap<Object,Object>(); 
  6.  
  7.    TreeMap<Object,Object> treeMap = new TreeMap<Object,Object>(); 
  8.  
  9.    list.add("a"); 
  10.  
  11.    list.add("b"); 
  12.  
  13.    list.add("c"); 
  14.  
  15.    
  16.  
  17.    hash.put(3, 3); 
  18.  
  19.    hash.put(4, 4); 
  20.  
  21.    hash.put(5, 5); 
  22.  
  23.    hash.put(6, 6); 
  24.  
  25.    hash.put(1, 1); 
  26.  
  27.    hash.put(2, 2); 
  28.  
  29.    
  30.  
  31.    treeMap.put(1, 1); 
  32.  
  33.    treeMap.put(2, 2); 
  34.  
  35.    treeMap.put(3, 3); 
  36.  
  37.    treeMap.put(4, 4); 
  38.  
  39.    treeMap.put(5, 5); 
  40.  
  41.    treeMap.put(6, 6); 
  42.  
  43.    
  44.  
  45.    //list遍历 
  46.  
  47.    for(String m: list){ 
  48.  
  49.     System.out.println(m); 
  50.  
  51.    } 
  52.  
  53.    // hashmap entrySet() 遍历 
  54.  
  55.    for(Map.Entry<Object,Object> m: hash.entrySet()){ 
  56.  
  57.     System.out.println(m.getKey()+"---"+m.getValue()); 
  58.  
  59.    } 
  60.  
  61.    //hashmap keySet() 遍历 
  62.  
  63.    for(Object m: hash.keySet()){ 
  64.  
  65.     System.out.println(m+"---"+hash.get(m)); 
  66.  
  67.    } 
  68.  
  69.    // treemap keySet()遍历 
  70.  
  71.    for(Object m: treeMap.keySet()){ 
  72.  
  73.     System.out.println(m+"---"+treeMap.get(m)); 
  74.  
  75.    } 
  76.  

 

 

以上内容转自网络:http://blog.csdn.net/szwangdf/article/details/2544143

0 0
原创粉丝点击