资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

HashMap集合遍历的四种方式现金盘源码出售搭建


现金盘源码出售搭建【hubawl.com】狐霸源码论坛

创新互联公司长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为临县企业提供专业的成都网站设计、成都网站制作临县网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。


对于Map来说,遍历的方式都是一样的,大概都是有四种形式

直接遍历

返回keySet()

返回Values()

返回entrySet()


对于第四种方式可能会除了返回的可以直接打印外,还可以通过返回Map.Entry类来依次遍历该集合返回key和value值

import java.util.*;


public class HashMapDemo {

    public static void main(String[] args) {

        Map hm = new HashMap<>();

        hm.put("姓名", "Jack");

        hm.put("age", 18);

        hm.put("身高", '?');

        hm.put("身高", 178);

        // 键和值都允许为 null

        hm.put(null, null);


        // 第一种遍历方式 : 直接输出该对象

        System.out.println("直接打印");

        System.out.println(hm);


        // 第二种遍历方式 : 通过返回键集

        System.out.println("keySet()");

        Set keySet = hm.keySet();

        Iterator iter = keySet.iterator();

        while (iter.hasNext()) {

            Object next = iter.next();

            System.out.println(next + "," + hm.get(next));

        }


        /*

首先返回的都是集合类型,除了可以使用iterator进行遍历外,还可以使用增强for循环也是非常方便的.

其实增强for循环就是上面形式的简写.本质也是上面的形式.

*/

        // 增强for循环

        System.out.println("增强for循环");

        for (Object s :

                keySet) {

            System.out.println(s + "," + hm.get(s));

        }


        // 第三种遍历方式 : 通过返回值集

        System.out.println("value()");

        Collection values = hm.values();

        Iterator iter1 = values.iterator();

        while (iter1.hasNext()) {

            System.out.println(iter1.next());

        }


        // 第四种遍历方式 : 通过返回key-value集

        System.out.println("entrySet()集");

        Iterator iter2 = hm.entrySet().iterator();

        while (iter2.hasNext()) {

            System.out.println(iter2.next());

        }


        /*

        第四种方式的细化,将返回的key-value集分别取出

         */

        while (iter2.hasNext()) {

            Map.Entry next = (Map.Entry) iter2.next();

            System.out.println(next.getKey() + " . " + next.getValue());

        }


        /*

        第四种方式细化的简化形式.

         */

        Set entrySet = hm.entrySet();

        for (Map.Entry e : entrySet) {

            System.out.println(e.getKey() + " , " + e.getValue());

        }

    }

}


        

上面代码的反编译 :


import java.io.PrintStream;

import java.util.*;


public class HashMapDemo

{


public HashMapDemo()

{

}


public static void main(String args[])

{

Map hm = new HashMap();

hm.put("姓名", "Jack");

hm.put("age", Integer.valueOf(18));

hm.put("身高", Character.valueOf('?'));

hm.put("身高", Integer.valueOf(178));

hm.put(null, null);

System.out.println("直接打印");

System.out.println(hm);

System.out.println("keySet()");

Set keySet = hm.keySet();

Object next;

for (Iterator iter = keySet.iterator(); iter.hasNext(); 

System.out.println((new StringBuilder()).append(next).append(",").append(hm.get(next)).toString()))

next = iter.next();


System.out.println("增强for循环");

Object s;

for (Iterator iterator = keySet.iterator(); iterator.hasNext(); 

System.out.println((new StringBuilder()).append(s).append(",").append(hm.get(s)).toString()))

s = iterator.next();


System.out.println("value()");

Collection values = hm.values();

for (Iterator iter1 = values.iterator(); iter1.hasNext(); System.out.println(iter1.next()));


System.out.println("entrySet()集");

Iterator iter2;

for (iter2 = hm.entrySet().iterator(); iter2.hasNext(); System.out.println(iter2.next()));

java.util.Map.Entry next;

for (; iter2.hasNext(); 

System.out.println((new StringBuilder()).append(next.getKey()).append(" . ").append(next.getValue()).toString()))

next = (java.util.Map.Entry)iter2.next();


Set entrySet = hm.entrySet();

java.util.Map.Entry e;

for (Iterator iterator1 = entrySet.iterator(); iterator1.hasNext(); 

System.out.println((new StringBuilder()).append(e.getKey()).append(" , ").append(e.getValue()).toString()))

e = (java.util.Map.Entry)iterator1.next();


}

}

--------------------- 

版权声明:本文为CSDN博主「-Clearlight」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_36852780/article/details/98470429


网页名称:HashMap集合遍历的四种方式现金盘源码出售搭建
网站网址:http://cdkjz.cn/article/gdeshg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220