查找具有最低整数值的HashMap的键 [英] Finding the key of HashMap which holds the lowest integer value

查看:178
本文介绍了查找具有最低整数值的HashMap的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为需要学习最常用单词的年轻学生创建一个教育游戏.我随机选择列表中的三个单词,在屏幕上显示它们,播放三个单词之一的录音,然后学生必须选择已发音的单词.我跟踪他们猜过每个单词多少次.这样,我可以为何时向学生介绍新单词设置标准.当三一挑,我会喜欢这个词的发音,学生不得不至少在曝光的话.

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 recording of one of the three words and then the student has to pick the word that has been pronounced. I keep track of how many times they have guessed each word. In that way i can set up a criteria for when new words should be introduced to the student. When three of the words a picked I'll like to pronounce the word that the student has had least exposure to.

我有一个称为单词的HashMap,其中包含单词,以及一个整数,表示学生猜出单词的次数.

I have a HashMap called words, which contains the words, and a integer value of how many times the student guessed the word.

  HashMap<String,Integer>  words 

它包含10到120个键/单词.我想创建一个方法,该方法将三个哈希映射键作为参数,该方法可以返回具有所要求键的最小值的String/键.

It contains between 10 - 120 keys/words. I'll like to create a method, which takes three of the hash map keys as parameters, that can return the String/key having the lowest value of the keys asked for.

我有团体让它按预期工作,感谢您的帮助.

I have had trouples getting this to work as intended and I'd appreciate any help.

推荐答案

这怎么办?

private String getMinKey(Map<String, Integer> 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;
}

这篇关于查找具有最低整数值的HashMap的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆