[英]Java Hashtable set all keys with same value
I am currently working in java and using a hashtable with keys and values. 我目前在Java中工作,并使用具有键和值的哈希表。
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. 我想要做的是浏览文件,并将每个单词作为键放入哈希表中,并有一定的概率应将其初始化为1 /总单个单词。
Currently my code loops trhough the file and for every word does: map.put(word,0)
. 目前,我的代码
map.put(word,0)
文件,每个单词都执行: 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). (值被初始化为0,因为我仍然不知道单词总数,因为它仍在循环中)。 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?
在此循环的最后,我想在哈希表1 / map.size()中设置每个值,是否有一种简单的方法可以做到这一点,而不必遍历所有哈希表键?
Thanks in advance! 提前致谢!
如果您使用的是Java 8+,则可以使用replaceAll :
map.replaceAll((k, v) -> 1.0 / map.size());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.