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

      Map集合遍历

      Map<String,Integer> m = new HashMap<String,Integer>();

        m.put("one",100);

        m.put("two",200);

        m.put("three",300);

    法一:

      法一涉及到的方法keySet()

        for(String s : m.keySet){               //keySet()获取key的集合;

        System.out.print("keys: "+s);

        System.out.print("values: "+m3.get(s)); //并通过get(Object key)获取对应的value的值。

      }

    法二:

      法二涉及到的方法values()

      for(Integer t : m.values()){

        System.out.print("values: "+t);   //values()获取value的集合

      }

    法三:

      法三涉及到Map接口的内部接口Entry接口,涉及到Entry接口的getKey()方法和getValue()方法

      for(Map.Entry<String,Integer> entry : m.entrySet()){

        System.out.print("keys: "+entry.getKey()+" values: "+entry.getValue()); 

      }

    法四:

      使用迭代器,涉及到Map接口的内部接口Entry接口,涉及到Entry接口的getKey()方法和getValue()方法

      Set set = m.entrySet();

      Iterator it = set.iterator();

      while(it.hasNext()){

        Map.Entry<String,Integer> entry = (Map.Entry<String,Integer> entry)it.next();

        System.out.print("keys: "+entry.getKey()+" values: "+entry.getValue()); 

      }

  • 相关阅读:
    amfphp1.9 class mapping初探
    C#程序打包.exe应用程序
    数据库备份方案
    ListView 控件使用
    在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库
    .NET
    转载:MATLAB 符号函数作图
    整理雷达相关
    Python 程序 运行过程
    struts2 文件上传显示进度条
  • 原文地址:https://www.cnblogs.com/suancaipaofan/p/6226695.html
Copyright © 2011-2022 走看看