首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Java Stream打印文本文件中最常用的单词?

如何使用Java Stream打印文本文件中最常用的单词?
EN

Stack Overflow用户
提问于 2018-10-18 01:44:25
回答 2查看 0关注 0票数 0

我试图弄清楚如何用显示最常用单词的流替换for循环。文本文件是“Sophie Sally和Jack是dachshunds Dachshunds是最好的狗,所有的狗都比猫好”并且输出应该是“出现2次的词:并且是dachshunds狗”。我已经尝试重新使用和更改其他流的逻辑,但我无法做任何工作。需要更改为流的代码的唯一部分是底部的for循环,这是影响“and dachshunds dogs”部分的部分,这是文本文档中最常用的单词。

代码语言:javascript
复制
    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);
                }
            }
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2018-10-18 09:47:15

TODO使用单个语句替换以下循环,使用流打印文档中最常用的单词 for (String k : wordFrequency.keySet()) { if (maxCnt == wordFrequency.get(k)) { System.out.print(" " + k); } } } }

流版本:

代码语言:javascript
复制
wordFrequency.entrySet().stream()
             .filter(e -> e.getValue() == maxCnt)
             .forEach(e -> System.out.print(" " + e.getKey()));
票数 0
EN

Stack Overflow用户

发布于 2018-10-18 10:46:13

以下代码将执行此操作。

代码语言:javascript
复制
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();

你也可以这样做打印部分:

代码语言:javascript
复制
long count = highestCount.getKey();
String words = highestCount.getValue().stream().collect(Collectors.joining(" "));
System.out.println("Words that appear " + count + " times: " + words);

两者的输出

代码语言:javascript
复制
Words that appear 2 times: and are dachshunds dogs
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档