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         }

    输出:

  • 相关阅读:
    Java程序员之JS(一) 入门
    Java虚拟机(一)之开篇
    JDK/JRE/JVM区别与联系
    web开发视频(一)之环境准备
    Spring MVC 教程,快速入门,深入分析
    Java中“==和equals”的区别
    如何查看电脑最大支持多少GB内存
    win10 计算器calc命令打不开
    Win10图标显示不正常解决办法
    在系统右键菜单上添加程序
  • 原文地址:https://www.cnblogs.com/maduar/p/4357375.html
Copyright © 2011-2022 走看看