找到保存最小整数值的 HashMap 的键

时间:2022-10-13
本文介绍了找到保存最小整数值的 HashMap 的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在为需要学习最常用单词的年轻学生创建一个教育游戏.我随机选择列表中的三个单词,将它们显示在屏幕上,播放三个单词之一的录音,然后学生必须选择已经发音的单词.我记录了他们每个单词猜了多少次.通过这种方式,我可以为何时应该向学生介绍新单词设置一个标准.When three of the words are picked I'll like to pronounce the word that the student has had least exposure to.

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 are picked I'll like to pronounce the word that the student has had least exposure to.

我有一个名为 words 的 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 个键/词.我想创建一个方法,它将三个哈希映射键作为参数,它可以返回具有最低要求键值的字符串/键.

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 trouple 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 的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:如何在不打印重复项的同时从 HashMap 打印值? 下一条:Map Java 的递归迭代

相关文章

最新文章