第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

為什么 LinkedHashMap 不能對 HashMap 進(jìn)行排序而 TreeMap 可以?

為什么 LinkedHashMap 不能對 HashMap 進(jìn)行排序而 TreeMap 可以?

Qyouu 2021-09-15 14:33:37
我正在嘗試HashMap使用LinkedHashMapand對輸出進(jìn)行排序TreeMap。當(dāng)我TreeMap用來整理HashMap它時(shí),它就像一種魅力。        Map<Integer, String> hMap = new HashMap<Integer, String>();        hMap.put(40, "d");        hMap.put(10, "a");        hMap.put(30, "c");        hMap.put(20, "b");        System.out.println(" ");        System.out.println("before");        for (Map.Entry m1 : hMap.entrySet()) {            System.out.print(m1.getKey() + " " + m1.getValue() + "    ");        }        System.out.println("after");        Map<Integer, String> hTree = new TreeMap<Integer, String>(hMap);        for (Map.Entry m2 : hTree.entrySet()) {            System.out.print(m2.getKey() + " " + m2.getValue() + "    ");        }輸出 :before20 b    40 d    10 a    30 cafter10 a    20 b    30 c    40 d但是當(dāng)我嘗試LinkedHashMap排序時(shí),HashMap它似乎不起作用。        Map<Integer, String> hMap = new HashMap<Integer, String>();        hMap.put(10, "a");        hMap.put(20, "b");        hMap.put(30, "c");        hMap.put(40, "d");        System.out.println("before");        for (Map.Entry m1 : hMap.entrySet()) {            System.out.print(m1.getKey() + " " + m1.getValue() + "    ");        }        System.out.println(" ");        System.out.println("after");        LinkedHashMap<Integer, String> lhMap = new LinkedHashMap<Integer, String>(hMap);        Iterator it = lhMap.entrySet().iterator();        while (it.hasNext()) {            Map.Entry me = (Map.Entry) it.next();            System.out.print(me.getKey() + " " + me.getValue()+"   ");        }輸出 :before20 b    40 d    10 a    30 c     after20 b   40 d   10 a   30 c  誰能告訴我為什么這種排序不起作用?那是因?yàn)長inkedHashMap正在過濾HashMap嗎?如果是為什么TreeMap對那個(gè)問題免疫?
查看完整描述

3 回答

  • 3 回答
  • 0 關(guān)注
  • 200 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號