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

    Map<String,Object> map = new HashMap<>();
    map.put("1",1);
    map.put("2",2);
    map.put("3",3);
    // 第一种遍历,根据keySet()方法
    System.out.println("第一种遍历方法:");
    for(String key : map.keySet()) {
    Object obj = map.get(key);
    System.out.println(obj);
    }

    // 第二种遍历,利用迭代器map.entrySet().iterator()
    System.out.println("第二种遍历方法:");
    Iterator<Map.Entry<String,Object>> entryIterator = map.entrySet().iterator();
    while (entryIterator.hasNext()) {
    Map.Entry<String,Object> entry = entryIterator.next();
    System.out.println("entry.getKey():" + entry.getKey());
    System.out.println("entry.getValue()" + entry.getValue());
    }

    // 第三种遍历方法,推荐,简便且合适大容量数值
    System.out.println("第三种遍历方法:");
    for(Map.Entry<String,Object> entry : map.entrySet()) {
    System.out.println("entry.getkey():" + entry.getValue());
    }
  • 相关阅读:
    Python阶段复习
    Python阶段复习
    Python学习笔记
    Python爬虫学习
    Python爬虫学习
    Python学习笔记
    史上最全的Maven Pom文件标签详解
    css3 animation动画技巧
    常用的sass编译库
    compass做雪碧图
  • 原文地址:https://www.cnblogs.com/xiaoyu666/p/9719862.html
Copyright © 2011-2022 走看看