Obtain a random value from a map with reverse weights in Java

0

I have a map from which I want to get a word for a game of the hangman. In the map I keep with each word the times that have already been chosen. For example, the map may contain:

abaleadura  4
abajo 2
abalada 1
abalaustrado 3

The map starts with all the words to zero.

The question is: How to get a word randomly so that those that have least appeared are more likely to leave? I have tried to repeat the words in a list and get a word from that list but I have trouble knowing how many times I have to repeat each word. Greetings and thanks.

    
asked by Miguel-David 23.05.2018 в 20:33
source

1 answer

0

Instead of using the common Map, use the other type called TreeMap, where in this, the keys of the values are sorted automatically from low to high.

Once you have the TreeMap with the words, use the firstKey () method on your TreeMap to return the first key on the MapMap.

Then using the get (key) method, which will return the value of that key. In this way you can always acquire the word that less appearances had

    
answered by 29.05.2018 в 16:34