zoukankan      html  css  js  c++  java
  • java の map集合遍历

    遍历map集合

    1. map的keySet()方法返回集合中所有键对象的集合。
    2. mapentrySet()方法一个存放了Map.Entry类型的set集合,每个Map.Entry对象代表map中的一对键与值。、
     //Map遍历
        public static void main(String[] args)
        {
            Map<String,String> map = new HashMap<String,String>();
            map.put("a", "monday");
            map.put("b", "wednesday");
            map.put("c", "thursday");
            map.put("d", "tuesday");
            //方法1
            Set<String> set = map.keySet();
            Iterator<String> it = set.iterator();
            while(it.hasNext()){
                String key = it.next();
                String value = map.get(key);
                System.out.println(value);
            }
            //方法2
            Iterator<Map.Entry<String,String>> its = map.entrySet().iterator();
            while(its.hasNext()){
                Map.Entry<String,String> m= its.next();
                System.out.println(m.getKey()+":::"+m.getValue());
            }
        }

     java主要集合的框架图

    Collection接口的方法

    Iterator接口隐藏底层集合的数据结构,向客户程序提供了遍历各种类型的集合的统一接口。Iterator接口中声明了如下方法:

    *   hasNext() : 判断集合中的元素是否遍历完毕,如果没有,就返回true.

    * next() : 返回下一个元素。

    * remove() : 从集合中删除上一个由next()方法返回的元素。

  • 相关阅读:
    单例模式
    反射常见方法
    字符流,字节流总结
    泛型限定
    随机数判断最值
    字符流与字节流练习
    文件常见操作属性
    文件过滤器
    字符流读取文件
    目前最流行的IT编程语言
  • 原文地址:https://www.cnblogs.com/zyoohoo/p/2540464.html
Copyright © 2011-2022 走看看