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

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

在Java映射中查找與最大值關(guān)聯(lián)的鍵

在Java映射中查找與最大值關(guān)聯(lián)的鍵

明月笑刀無情 2019-10-26 12:46:54
獲取與映射中的最大值關(guān)聯(lián)的鍵的最簡單方法是什么?我相信,當您想要對應(yīng)于最大值的鍵時,Collections.max(someMap)將返回最大鍵。
查看完整描述

3 回答

?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

基本上,您需要遍歷地圖的條目集,同時記住“當前已知的最大值”和與之相關(guān)的鍵。(當然,或者僅包含兩者的條目。)


例如:


Map.Entry<Foo, Bar> maxEntry = null;


for (Map.Entry<Foo, Bar> entry : map.entrySet())

{

    if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)

    {

        maxEntry = entry;

    }

}


查看完整回答
反對 回復(fù) 2019-10-26
?
www說

TA貢獻1775條經(jīng)驗 獲得超8個贊

為了完整起見,這是一種Java-8方式


countMap.entrySet().stream().max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).get().getKey();

要么


Collections.max(countMap.entrySet(), (entry1, entry2) -> entry1.getValue() - entry2.getValue()).getKey();

要么


Collections.max(countMap.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();


查看完整回答
反對 回復(fù) 2019-10-26
?
米脂

TA貢獻1836條經(jīng)驗 獲得超3個贊

該代碼將打印所有具有最大值的鍵


public class NewClass4 {

    public static void main(String[] args)

    {

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

        map.put(1, 50);

        map.put(2, 60);

        map.put(3, 30);

        map.put(4, 60);

        map.put(5, 60);

        int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap

        for (Entry<Integer, Integer> entry : map.entrySet()) {  // Itrate through hashmap

            if (entry.getValue()==maxValueInMap) {

                System.out.println(entry.getKey());     // Print the key with max value

            }

        }


    }

}


查看完整回答
反對 回復(fù) 2019-10-26
  • 3 回答
  • 0 關(guān)注
  • 450 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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