zoukankan      html  css  js  c++  java
  • Map迭代(六种)

     1 Map<Integer,String> map = new HashMap<Integer,String>();
     2         map.put(1, "a");
     3         map.put(2, "b");
     4         
     5         System.out.println("(1):");
     6         //1.Map输出1(只遍历一次,速度快)
     7         Iterator itr11 = map.entrySet().iterator();
     8         while(itr11.hasNext()){
     9             Map.Entry e = (Map.Entry)itr11.next();
    10             System.out.println(e.getKey()+"==="+e.getValue());
    11         }
    12         System.out.println();
    13         System.out.println("(2):");
    14         //2.Map输出2(速度慢)
    15         Set set = map.keySet();//用接口实例接口
    16         Iterator itr22 = set.iterator();
    17         while(itr22.hasNext()){//遍历二次,速度慢
    18             Integer key = (Integer)itr22.next();
    19             System.out.println(key+"==="+map.get(key));
    20         }
    21         //3.Map输出3(for语句)
    22         System.out.println();
    23         System.out.println("(3):");
    24         Set<Integer> set00 = map.keySet();
    25         for(Integer s :set00){
    26             System.out.println(s+"==="+map.get(s));
    27         }
    28         
    29         //4.Map输出4
    30         System.out.println();
    31         System.out.println("(4):");
    32         Iterator itr44 = map.values().iterator();        
    33         while(itr44.hasNext()){
    34             System.out.println(itr44.next());
    35         }
    36         System.out.println();
    37         
    38         //5.Map输出5
    39         System.out.println("(5):");
    40         System.out.println(map);
    41         
    42         //6.Map输出6
    43         System.out.println();
    44         System.out.println("(6):");
    45         for(Map.Entry en:map.entrySet()){
    46             System.out.println(en.getKey()+"==="+en.getValue());
    47         }

    输出:

  • 相关阅读:
    scrapy(二)内容获取
    scrapy(一)建立一个scrapy项目
    scrapy(四)使用redis
    scrapy(三)使用mongoDB
    索引处的解码字符串
    Golang竞争状态
    Golang之泛型编程-细节
    区块链学这个就够了-DLT(一)
    Linux日志分析-Ubuntu(一)
    经典博弈-int
  • 原文地址:https://www.cnblogs.com/maduar/p/4357375.html
Copyright © 2011-2022 走看看