zoukankan      html  css  js  c++  java
  • JAVA 遍历Map的四种方法

    public static void main(String[] args) {


      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      
      //第一种:通过Map.keySet遍历,普遍使用,二次取值
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      
      //第二种:通过Map.entrySet使用iterator遍历
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      
      //第三种:推荐,尤其是容量大时
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }

      //第四种:只能取值,不能遍历KEY
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }

  • 相关阅读:
    AxInterop.VPIClient DLL注册
    多个事务同时操作数据库
    aspx小试
    WPF 或得PNG图片的外形Path的Data
    Spass导出数据
    Excel VBA小试
    合并Excel文件
    asp.net 中文编码问题
    Delphi中的容器类(3)
    Delphi中的容器类(1)
  • 原文地址:https://www.cnblogs.com/laohuihui/p/5308771.html
Copyright © 2011-2022 走看看