溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HashMap集合遍歷的四種方式 現(xiàn)金盤源碼出售搭建

發(fā)布時(shí)間:2020-08-05 03:29:35 來源:ITPUB博客 閱讀:155 作者:dfhdfh534534 欄目:web開發(fā)


現(xiàn)金盤源碼出售搭建【hubawl.com】狐霸源碼論壇


對(duì)于Map來說,遍歷的方式都是一樣的,大概都是有四種形式

直接遍歷

返回keySet()

返回Values()

返回entrySet()


對(duì)于第四種方式可能會(huì)除了返回的可以直接打印外,還可以通過返回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);


        // 第一種遍歷方式 : 直接輸出該對(duì)象

        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進(jìn)行遍歷外,還可以使用增強(qiáng)for循環(huán)也是非常方便的.

其實(shí)增強(qiáng)for循環(huán)就是上面形式的簡(jiǎn)寫.本質(zhì)也是上面的形式.

*/

        // 增強(qiáng)for循環(huán)

        System.out.println("增強(qiáng)for循環(huán)");

        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());

        }


        /*

        第四種方式的細(xì)化,將返回的key-value集分別取出

         */

        while (iter2.hasNext()) {

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

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

        }


        /*

        第四種方式細(xì)化的簡(jiǎn)化形式.

         */

        Set<Map.Entry> 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("增強(qiáng)for循環(huán)");

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();


}

}

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

版權(quán)聲明:本文為CSDN博主「-Clearlight」的原創(chuàng)文章,遵循CC 4.0 by-sa版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。

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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI