我试图弄清楚如何用显示最常用单词的流替换for循环。文本文件是“Sophie Sally和Jack是dachshunds Dachshunds是最好的狗,所有的狗都比猫好”并且输出应该是“出现2次的词:并且是dachshunds狗”。我已经尝试重新使用和更改其他流的逻辑,但我无法做任何工作。需要更改为流的代码的唯一部分是底部的for循环,这是影响“and dachshunds dogs”部分的部分,这是文本文档中最常用的单词。
import java.io.FileNotFoundException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class FrequentWords {
public static void main(String[] args) throws FileNotFoundException
{
String filename = "SophieSallyJack.txt";
if (args.length == 1) {
filename = args[0];
}
Map<String, Integer> wordFrequency = new TreeMap<>();
List<String> 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 = wordFrequency.values().stream().max(Comparator.naturalOrder()).get();
System.out.print("Words that appear " + maxCnt + " times:");
// TODO replace the following loop with a single statement using streams
// that prints the most frequent words in the document
for (String k : wordFrequency.keySet()) {
if (maxCnt == wordFrequency.get(k)) {
System.out.print(" " + k);
}
}
}
}
发布于 2018-10-18 09:47:15
TODO使用单个语句替换以下循环,使用流打印文档中最常用的单词
for (String k : wordFrequency.keySet()) { if (maxCnt == wordFrequency.get(k)) { System.out.print(" " + k); } } } }
流版本:
wordFrequency.entrySet().stream()
.filter(e -> e.getValue() == maxCnt)
.forEach(e -> System.out.print(" " + e.getKey()));
发布于 2018-10-18 10:46:13
以下代码将执行此操作。
String input = "Sophie Sally and Jack were dachshunds Dachshunds are the best dogs and all dogs are better than cats";
Entry<Long, List<String>> highestCount = Pattern.compile("\\W+").splitAsStream(input)
.filter(word -> ! word.isEmpty())
.map(String::toLowerCase)
// at this point we're streaming a list of lowercase Words
.collect(Collectors.groupingBy(Function.identity(), TreeMap::new,
Collectors.counting()))
// now we have a TreeMap<String, Long> mapping Word to Count
.entrySet().stream()
.collect(Collectors.groupingBy(Entry::getValue, TreeMap::new,
Collectors.mapping(Entry::getKey, Collectors.toList())))
// now we have a TreeMap<Long, List<String>> mapping Count to list of Words
.lastEntry(); // Since it's a TreeMap, the last entry is the highest Count
System.out.print("Words that appear " + highestCount.getKey() + " times:");
highestCount.getValue().forEach(word -> System.out.print(" " + word));
System.out.println();
你也可以这样做打印部分:
long count = highestCount.getKey();
String words = highestCount.getValue().stream().collect(Collectors.joining(" "));
System.out.println("Words that appear " + count + " times: " + words);
两者的输出
Words that appear 2 times: and are dachshunds dogs
https://stackoverflow.com/questions/-100002934
复制相似问题