Map的遍历获取key和vlue的两种方法

Map的遍历获取key和vlue的两种方法


方式一:


    @Test
    public void forMap(){
        Map<String, String> map = new HashMap<String, String>();
        map.put(null, null);
        map.put("xxx", null);
        map.put("aaa", "111");
        map.put("bbb", "222");
        map.put("ccc", "333");

        Iterator<Entry<String, String>> iterator  = map.entrySet().iterator();

        while (iterator.hasNext()) {

            Entry<String, String> entry = iterator.next();

            System.out.println(entry.getValue()+":"+entry.getValue());

        }

    }

方式二:
方式二比较适合用于数据大使用

    @Test
    public void t1(){
        Map<String, String> map = new HashMap<String, String>();
        map.put(null, null);
        map.put("xxx", null);
        map.put("aaa", "111");
        map.put("bbb", "222");
        map.put("ccc", "333");
        System.out.println(map);

        //mapd的遍历
        for(Entry<String, String> entry:map.entrySet()){

            System.out.println(entry.getKey()+":"+entry.getValue());
        }

    }*/

猜你喜欢

转载自blog.csdn.net/zx6571269/article/details/80168134
今日推荐