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

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    public class TestMap {
        public static void main(String[] args) {
            Map《Integer, String》 map = new HashMap《Integer, String》();
            map.put(1, "a");
            map.put(2, "b");
            map.put(3, "ab");
            map.put(4, "ab");
            map.put(4, "ab");// 和上面相同 , 会自己筛选
            System.out.println(map.size());
            // 第一种:
           
            System.out.println("第一种:通过Map.keySet遍历key和value:");
            for (Integer in : map.keySet()) {
                //map.keySet()返回的是所有key的值
                String str = map.get(in);//得到每个key多对用value的值
                System.out.println(in + "     " + str);
            }
            // 第二种:
            System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");
            Iterator《Map.Entry《Integer, String》》 it = map.entrySet().iterator();
            while (it.hasNext()) {
                 Map.Entry《Integer, String》 entry = it.next();
                   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            }
            // 第三种:推荐,尤其是容量大时
            System.out.println("第三种:通过Map.entrySet遍历key和value");
            for (Map.Entry《Integer, String》 entry : map.entrySet()) {
                //Map.entry《Integer,String》 映射项(键-值对)  有几个方法:用上面的名字entry
                //entry.getKey() ;entry.getValue(); entry.setValue();
                //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
                System.out.println("key= " + entry.getKey() + " and value= "
                        + entry.getValue());
            }
            // 第四种:
            System.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");
            for (String v : map.values()) {
                System.out.println("value= " + v);
            }
        }
    }
  • 相关阅读:
    【LeetCode】589.N叉树的前序遍历(递归+迭代,java实现,详细分析)
    百度网盘偷偷更新,终于实现免费不限速了!
    如何调整DOS窗口的宽高
    输入adb shell 时 提示error: more than one device and emulator
    logcat不显示信息
    安卓打开File Explorer里面不显示内容
    android查看源码的时候看不了
    This version of the rendering library is more recent than your version of ADT plug-in. Please update
    eclipse或者AS链接手机真机之后,logcat里面日志信息乱跳
    Android ADB使用之详细篇
  • 原文地址:https://www.cnblogs.com/luckForever/p/7254103.html
Copyright © 2011-2022 走看看