[JAVA]JAVA遍历Map的几种方式

     //遍历key
        for (String key : dic.keySet()
                ) {
            System.out.println(key + dic.get(key));
        }
        //遍历values
        for (String value : dic.values()
                ) {
            System.out.println(value);
        }
        //使用entry遍历键值对
        for (Map.Entry entry:dic.entrySet()
             ) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }

        //用迭代器遍历
        Iterator> itor = dic.entrySet().iterator();
        while (itor.hasNext())
        {
            Map.Entry entry =  itor.next();
            System.out.println(entry.getKey()+""+entry.getValue());
        }    

  

你可能感兴趣的:([JAVA]JAVA遍历Map的几种方式)