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

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

更新 HashMap 的現(xiàn)有鍵

更新 HashMap 的現(xiàn)有鍵

BIG陽(yáng) 2023-02-23 11:12:49
我有長(zhǎng)度為 3 的 HashMap < Integer,String>:1=>"Value1"2=>"Value2"3=>"Value3"現(xiàn)在我想將所有鍵減 1(如果鍵>1): 輸出:1=>"Value2"2=>"Value3"我在嘗試什么  for (e in hashMap.entries) {                                val entry = e as Map.Entry<*, *>                                var keyPos = (entry.key as Int)                                if (keyPos != -1) {                                    if (keyPos > 1) {                                        keyPos = keyPos - 1                                        if (keyPos != -1) {                                            hashMap.put(keyPos, entry.value as String?)                                        }                                    }                                }                            }但它沒(méi)有提供所需的輸出。如何使其在沒(méi)有并發(fā)異常的情況下工作。
查看完整描述

3 回答

?
鴻蒙傳說(shuō)

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

另一種方法是使用mapKeys擴(kuò)展函數(shù),它允許您重新定義映射條目的鍵:

fun main() {

    val originalMap = mapOf(1 to "value1", 2 to "value2", 3 to "value3")

    val updatedMap = originalMap

        .mapKeys {

            if (it.key > 1) {

                it.key - 1

            } else {

                it.key

            }

        }


    println(updatedMap) // prints: {1=value2, 2=value3}

}

請(qǐng)注意,這不會(huì)就地更新地圖,但會(huì)創(chuàng)建一個(gè)新地圖。另請(qǐng)注意:

如果任何兩個(gè)條目映射到相同的鍵,則后一個(gè)的值將覆蓋與前一個(gè)關(guān)聯(lián)的值。

這意味著如果兩個(gè)鍵發(fā)生沖突,通常您無(wú)法知道哪個(gè)會(huì)“獲勝”(除非您使用的是LinkedHashMap,它保留了插入順序)。

更通用的方法是:

  1. 遞減所有鍵

  2. 過(guò)濾掉所有非正鍵

不過(guò),這將需要 2 次完整迭代(除非您使用Sequences,它是惰性求值的):

fun main() {

    val originalMap = mapOf(1 to "value1", 2 to "value2", 3 to "value3")

    val updatedMap = originalMap

        .mapKeys {

            it.key - 1

        }.filter {

            it.key > 0

        }


    println(updatedMap)

}


查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

這里與 Java 7 兼容代碼相同(無(wú)流)


HashMap<Integer, String> hashMap = new HashMap<>();

hashMap.put(1, "test1");

hashMap.put(2, "test2");

hashMap.put(3, "test3");


Map<Integer, String> yourNewHashMap = new HashMap<>();

for (final Map.Entry<Integer, String> entry : hashMap.entrySet()) {

    if (entry.getKey() != 1) { // make sure index 1 is omitted

        yourNewHashMap.put(entry.getKey() - 1, entry.getValue()); // decrease the index for each key/value pair and add it to the new map

    }

}

流的舊答案:


由于新的 Map 對(duì)象對(duì)你來(lái)說(shuō)沒(méi)問(wèn)題,我會(huì)使用以下流:評(píng)論是內(nèi)聯(lián)的


HashMap<Integer, String> hashMap = new HashMap<>();

hashMap.put(1, "test1");

hashMap.put(2, "test2");

hashMap.put(3, "test3");


// use this

Map<Integer, String> yourNewHashMap = hashMap.entrySet().stream()

                                       .filter(es -> es.getKey() != 1) // make sure index 1 is omitted

                                       .map(es -> new AbstractMap.SimpleEntry<Integer, String>(es.getKey() - 1, es.getValue())) // decrease the index for each key/value pair

                                       .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); // create a new map



查看完整回答
反對(duì) 回復(fù) 2023-02-23
?
偶然的你

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊

public static void main(String[] args) {

    HashMap<Integer, String> map = new HashMap<>();


    // Populate the HashMap

    map.put(1, "Value1");

    map.put(2, "Value2");

    map.put(3, "Value3");

    System.out.println("Original HashMap: "

            + map); 

    decreaseAllKeysByOne(map);

}


private static void decreaseAllKeysByOne(HashMap<Integer, String> map) {

    // Add your condition (if key>1)

    HashMap<Integer, String> newMap = new HashMap<>();

    map.remove(1);



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

    int i = 1;

    while (iterator.hasNext()) {

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

        newMap.put(i, entry.getValue());

        i++;

    }

    System.out.println("Modified HashMap: "

            + newMap);

}

輸出 :


原始 HashMap:{1=Value1, 2=Value2, 3=Value3}


修改后的 HashMap:{1=Value2, 2=Value3}


查看完整回答
反對(duì) 回復(fù) 2023-02-23
  • 3 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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