简体   繁体   中英

Java Hashtable set all keys with same value

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!

如果您使用的是Java 8+,则可以使用replaceAll

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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