Java 找到持有最小整数值的 HashMap 的键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21777745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:36:48  来源:igfitidea点击:

Finding the key of HashMap which holds the lowest integer value

javaiteratorhashmap

提问by Cyberlurk

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.

我正在为需要学习最常用单词的年轻学生创建一个教育游戏。我随机选择列表中的三个单词,将它们显示在屏幕上,播放三个单词之一的录音,然后学生必须选择已发音的单词。我会记录他们猜出每个单词的次数。通过这种方式,我可以设定何时应该向学生介绍新词的标准。当三个单词 a 被选中时,我会念出学生接触最少的单词。

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

我有一个名为 words 的 HashMap,其中包含单词和学生猜单词次数的整数值。

  HashMap<String,Integer>  words 

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.

它包含 10 - 120 个关键字/单词。我想创建一个方法,它将三个哈希映射键作为参数,它可以返回具有最低值的键值的字符串/键。

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

我遇到了麻烦让这个按预期工作,我很感激任何帮助。

采纳答案by Sim

What about this?

那这个呢?

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;
}

回答by Tim B

First get the entry set from the map:

首先从地图中获取条目集:

Set<Entry<String,Integer>> entries = map.entrySet();

Now dump that into an ArrayList so you can sort it:

现在将其转储到 ArrayList 中,以便您可以对其进行排序:

List<Entry<String,Integer>> sortedEntries = new ArrayList<>(entries);

Now sort the list:

现在对列表进行排序:

Collections.sort(sortedEntries, /*Define your comparitor here to compare by values */);

Your list now has the contents of the map sorted by value, you can access them in whatever order you like.

您的列表现在具有按值排序的地图内容,您可以按您喜欢的任何顺序访问它们。

回答by Ashwin

This is a variation of the answer of user3309578 static HashMap words = new HashMap();

这是 user3309578 static HashMap words = new HashMap(); 答案的变体。

private static String getMax () {
    String minKey = null;
    int minValue = Integer.MAX_VALUE;
    for (String key : words.keySet()) {
        int value = words.get(key);
        if (value < minValue) {
            minValue = value;
            minKey = key;
        }
    }
    return minKey;
}

public static void main (String[] args) {
    words.put("a", 2);
    words.put("b", 4);
    words.put("c", 6);
    words.put("d", 8);
    words.put("e", 1);
    words.put("f", 3);
    words.put("g", 5);
    words.put("h", 7);
    System.out.println(getMax());
}

回答by Jakub Kukul

Possibly the shortest solution, with Java 8:

可能是最短的解决方案,使用 Java 8:

private String getMinKey(Map<String, Integer> map, String... keys) {

    return map.entrySet().stream()
     .filter(p -> Arrays.asList(keys).contains(p.getKey()))
     .min(Comparator.comparingInt(Map.Entry::getValue)).get().getKey();

}

回答by yago

i made this, it can hold multiple keys=

我做了这个,它可以容纳多个键=

HashMap<String,Integer>hashmap_original=new HashMap<>();
        hashmap_original.put("key_1",1);
        hashmap_original.put("key_2",4);
        hashmap_original.put("key_3",1);
        hashmap_original.put("key_4",3);

        HashMap<String,Integer>hashmap_result=new HashMap<>();
        int int_minium_value = 9999; //put a maxium value that u know your code it wont pass it
        for (String String_key:hashmap_original.keySet()) {
            int int_compare_value=hashmap_original.get(String_key); //get the value

            if (int_compare_value<int_minium_value) {
                int_minium_value=int_compare_value;
                hashmap_result.clear(); //delete non min values
                hashmap_result.put(String_key,int_compare_value);
            } else if (int_compare_value==int_minium_value) {
                hashmap_result.put(String_key,int_compare_value);
            }

        }
        String result=null;//u can use a ArrayList
        for (String key_with_the_lowest_value : hashmap_result.keySet()) {
            if (result == null) {
                result = key_with_the_lowest_value;
            } else {
                result=result+","+key_with_the_lowest_value;
            }
        }