Finding the key of HashMap which holds the lowest integer value

后端 未结 5 834
暖寄归人
暖寄归人 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:52

    Possibly the shortest solution, with Java 8:

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

提交回复
热议问题