Finding the key of HashMap which holds the lowest integer value

后端 未结 5 874
暖寄归人
暖寄归人 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 22:00

    First get the entry set from the map:

    Set> entries = map.entrySet();
    

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

    List> 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.

提交回复
热议问题