Finding the key of HashMap which holds the lowest integer value

后端 未结 5 839
暖寄归人
暖寄归人 2021-01-21 21:07

I\'m creating an educational game for young students who needs to learn the most common words. On random I pick three words of the list, show them on the screen, play an audio r

5条回答
  •  滥情空心
    2021-01-21 21:46

    What about this?

    private String getMinKey(Map map, String... keys) {
        String minKey = null;
        int minValue = Integer.MAX_VALUE;
        for(String key : keys) {
            int value = map.get(key);
            if(value < minValue) {
                minValue = value;
                minKey = key;
            }
        }
        return minKey;
    }
    

提交回复
热议问题