对于我的任务,我必须用循环来替换循环,这些循环计算文本文档中单词的频率,并且我无法计算出TODO部分.
String filename = "SophieSallyJack.txt";
if (args.length == 1) {
filename = args[0];
}
Map wordFrequency = new TreeMap<>();
List incoming = Utilities.readAFile(filename);
wordFrequency = incoming.stream()
.map(String::toLowerCase)
.filter(word -> !word.trim().isEmpty())
.collect(Collectors.toMap(word -> word, word -> 1, (a, b) -> a + b, TreeMap::new));
int maxCnt = 0;
// TODO add a single statement that uses streams to determine maxCnt
for (String word : incoming) {
Integer cnt = wordFrequency.get(word);
if (cnt != null) {
if (cnt > maxCnt) {
maxCnt = cnt;
}
}
}
System.out.print("Words that appear " + maxCnt + " times:");
我试过这个:
wordFrequency = incoming.parallelStream().
collect(Collectors.toConcurrentMap(w -> w, w -> 1, Integer::sum));
但这不对,我不确定如何将maxCnt合并到流中.