Java Hashtable set all keys with same value

790 views Asked by At

I am currently working in java and using a hashtable with keys and values.

What I want to do is walk through a file and put every word as a key into the hashtable, with a certain probability that should be initialized as 1/total individual words.

Currently my code loops trhough the file and for every word does: map.put(word,0). (value is initlaized as 0 because I don't know the total number of words yet since it's still looping). At the end of this loop i'd like to make every value in the hashtable 1/map.size(), is there an easy way to do this instead of having to interate through all hashtable keys?

Thanks in advance!

1

There are 1 answers

0
Alex - GlassEditor.com On

You can use replaceAll if you are using Java 8+:

map.replaceAll((k, v) -> 1.0 / map.size());