zoukankan      html  css  js  c++  java
  • Map集合的遍历

     1 package Collection;
     2 
     3 import java.util.HashMap;
     4 import java.util.Iterator;
     5 import java.util.Map;
     6 import java.util.Set;
     7 
     8 import org.junit.Test;
     9 /**
    10  * 
    11  * @author Administrator
    12  *     遍历Map集合
    13  * Map<key,val>
    14  */
    15 public class TestMap {
    16     //把Map集合转成Set集合,Set集合有迭代器
    17 //    方法一:
    18     @Test
    19     public void testMap() {
    20         Map<String,Integer> map = new HashMap<String,Integer>();
    21         map.put("a",1);
    22         map.put("b",2);
    23         map.put("c",3);
    24         map.put("d",4);
    25     
    26         Set<String> set = map.keySet();
    27         Iterator<String> it=set.iterator();
    28         while(it.hasNext()) {
    29         Object o=it.next();
    30             System.out.println(o+"..."+map.get(o));
    31             
    32             /**输出
    33              *         a...1
    34              *        b...2
    35              *        c...3
    36              *        d...4
    37              */
    38         }
    39     }
    40     
    41 //    方法二:
    42     @Test
    43     public void testEntryset() {
    44         Map<String,Integer> map = new HashMap<String,Integer>();
    45         map.put("a",4);
    46         map.put("b",3);
    47         map.put("c",2);
    48         map.put("d",1);
    49     Set<Map.Entry<String, Integer>>     set=map.entrySet();
    50         Iterator<Map.Entry<String,Integer>> it= set.iterator();
    51         while(it.hasNext()) {
    52             Map.Entry<String, Integer> m=it.next();
    53             System.out.println(m.getKey()+"........."+m.getValue());
    54         }
    55         /**
    56          * 输出
    57          *             a.........4
    58                     b.........3
    59                     c.........2
    60                     d.........1
    61          * 
    62          */
    63     }
    64     
    65 }
  • 相关阅读:
    mysql 从库执行insert失败导致同步停止
    MySQL 占用cpu 100%
    MySQl 主从配置实战
    tomcat 线程数与 mysql 连接数综合调优
    mysql 数据迁移
    Windows系统上设置 Git Bash 的 Font 及 Locale
    java -jar 使用要点
    ConcurrentHashMap 从Java7 到 Java8的改变
    sql索引组织
    注册、启动、停止windows服务
  • 原文地址:https://www.cnblogs.com/lyjblogs/p/7885683.html
Copyright © 2011-2022 走看看