简体   繁体   English

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

[英]Finding the key of HashMap which holds the lowest integer value

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

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.它包含 10 - 120 个关键字/单词。 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.我有麻烦让它按预期工作,我很感激任何帮助。

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

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.您的列表现在具有按值排序的地图内容,您可以按您喜欢的任何顺序访问它们。

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();

}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM